From 8942dc7803e2e1898cc7de3f3a6b2f772595c179 Mon Sep 17 00:00:00 2001
From: Petar Heyken
Date: Fri, 17 Apr 2020 08:26:38 +0200
Subject: [PATCH] add errorCode to InvalidMessageException
---
.../java/postmark/client/HttpClientHandler.java | 2 +-
.../java/postmark/client/data/DataHandler.java | 17 +++++++++++++++++
.../exception/InvalidMessageException.java | 9 ++++++++-
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/wildbit/java/postmark/client/HttpClientHandler.java b/src/main/java/com/wildbit/java/postmark/client/HttpClientHandler.java
index ed86389..e47d664 100644
--- a/src/main/java/com/wildbit/java/postmark/client/HttpClientHandler.java
+++ b/src/main/java/com/wildbit/java/postmark/client/HttpClientHandler.java
@@ -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));
diff --git a/src/main/java/com/wildbit/java/postmark/client/data/DataHandler.java b/src/main/java/com/wildbit/java/postmark/client/data/DataHandler.java
index c759058..8f40d0c 100644
--- a/src/main/java/com/wildbit/java/postmark/client/data/DataHandler.java
+++ b/src/main/java/com/wildbit/java/postmark/client/data/DataHandler.java
@@ -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,
diff --git a/src/main/java/com/wildbit/java/postmark/client/exception/InvalidMessageException.java b/src/main/java/com/wildbit/java/postmark/client/exception/InvalidMessageException.java
index 639edc0..5d5f4a5 100644
--- a/src/main/java/com/wildbit/java/postmark/client/exception/InvalidMessageException.java
+++ b/src/main/java/com/wildbit/java/postmark/client/exception/InvalidMessageException.java
@@ -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;
}
}