Skip to content

Commit

Permalink
add errorCode to InvalidMessageException
Browse files Browse the repository at this point in the history
  • Loading branch information
pheyken committed Apr 17, 2020
1 parent b54a830 commit 8942dc7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void validateResponse(HttpClient.ClientResponse response) throws Postmar
throw new InvalidAPIKeyException(dataHandler.formatErrorMessage(message));

case 422:
throw new InvalidMessageException(dataHandler.formatErrorMessage(message));
throw new InvalidMessageException(dataHandler.formatErrorMessage(message), dataHandler.formatErrorCode(message));

case 500:
throw new InternalServerException(dataHandler.formatErrorMessage(message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public String formatErrorMessage(String data) throws IOException {
return node.get("Message").textValue();
}

/**
* Helper for filtering out error code returned by Postmark in case of HTTP status code 422
* @param data JSON object as String
* @return error code
* @throws IOException in case converting String to Object fails
*/
public Integer formatErrorCode(String data) throws IOException {
JsonNode node = fromJson(data, JsonNode.class);
JsonNode errorCodeNode = node.get("ErrorCode");

if (errorCodeNode == null || errorCodeNode.isNull()) {
return null;
}

return errorCodeNode.intValue();
}

/**
* Sets data mapper to be strict when making conversion of data to objects.
* If there is a mismatch between object and String in any other case than letter case,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
*/

public class InvalidMessageException extends PostmarkException {
public InvalidMessageException(String message) {
private final Integer errorCode;

public InvalidMessageException(String message, Integer errorCode) {
super(message);
this.errorCode = errorCode;
}

public Integer getErrorCode() {
return errorCode;
}
}

Expand Down

0 comments on commit 8942dc7

Please sign in to comment.