Skip to content

Commit

Permalink
Added error code to error messages that have error code. Separated er…
Browse files Browse the repository at this point in the history
…ror handling from http client handler. Added parameter usage to message details endpoint
  • Loading branch information
ibalosh committed May 6, 2020
1 parent 5193216 commit 4e66d04
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**/*.idea
**/*.iml
target
out
src/test/java/temp
src/test/resources/*properties*

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</developers>

<properties>
<postmark.version>1.5.2</postmark.version>
<postmark.version>1.5.3</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jackson.minimum.version>2.9.7</jackson.minimum.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.wildbit.java.postmark.client;

import com.wildbit.java.postmark.client.data.DataHandler;
import com.wildbit.java.postmark.client.data.model.PostmarkError;
import com.wildbit.java.postmark.client.exception.*;

import java.io.IOException;

/**
* Handlers errors returned by Http client. Main use of the class is to parse Postmark errors based on error content.
*/
public class HttpClientErrorHandler {
protected final DataHandler dataHandler;

public HttpClientErrorHandler(DataHandler dataHandler) {
this.dataHandler = dataHandler;
}
/**
*
* Validates HTTP request responses.
*
* @param statusCode - HTTP status code
* @param message - HTTP response message
* @throws PostmarkException in case invalid HTTP response is returned.
*/
public PostmarkException throwErrorBasedOnStatusCode(Integer statusCode, String message) throws IOException {
switch (statusCode) {
case 401:
return new InvalidAPIKeyException(postmarkErrorFromResponse(message));

case 408:
return new TimeoutException(message);

case 422:
return new InvalidMessageException(postmarkErrorFromResponse(message));

case 500:
return new InternalServerException(message);

default:
return new UnknownException(message);

}
}

/**
*
* Parse postmark error message from string
*
* @param message - error message
* @return - Postmark error object
* @throws IOException
*/
private PostmarkError postmarkErrorFromResponse(String message) throws IOException{
return dataHandler.fromJson(message, PostmarkError.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wildbit.java.postmark.client;

import com.wildbit.java.postmark.client.data.DataHandler;
import com.wildbit.java.postmark.client.data.model.PostmarkError;
import com.wildbit.java.postmark.client.exception.*;
import org.apache.log4j.spi.RootLogger;

Expand All @@ -16,10 +17,12 @@ public class HttpClientHandler {

private final HttpClient httpClient;
protected final DataHandler dataHandler;
private final HttpClientErrorHandler httpClientErrorHandler;
private boolean secureConnection = true;

protected HttpClientHandler(MultivaluedMap<String,Object> headers) {
this.dataHandler = new DataHandler(false);
this.httpClientErrorHandler = new HttpClientErrorHandler(this.dataHandler);
httpClient = new HttpClient(headers);
}

Expand Down Expand Up @@ -60,8 +63,13 @@ protected String execute(HttpClient.REQUEST_TYPES request_type, String url) thro
*/
protected String execute(HttpClient.REQUEST_TYPES request_type, String url, Object data) throws PostmarkException, IOException {
HttpClient.ClientResponse response = httpClient.execute(request_type, getSecureUrl(url), dataHandler.toJson(data));
validateResponse(response);
return response.getMessage();

if (response.getCode() == 200) {
return response.getMessage();
}
else {
throw httpClientErrorHandler.throwErrorBasedOnStatusCode(response.getCode(), response.getMessage());
}
}

/**
Expand Down Expand Up @@ -120,35 +128,4 @@ private String getSecureUrl(String url) {
String urlPrefix = this.secureConnection ? "https://" : "http://";
return urlPrefix + url;
}

/**
*
* Validates HTTP request responses.
*
* @param response HTTP request response
* @throws PostmarkException in case invalid HTTP response is returned.
*/
private void validateResponse(HttpClient.ClientResponse response) throws PostmarkException, IOException {
int code = response.getCode();
String message = response.getMessage();

switch (code) {

case 200: break;

case 401:
throw new InvalidAPIKeyException(dataHandler.formatErrorMessage(message));

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

case 500:
throw new InternalServerException(dataHandler.formatErrorMessage(message));

default:
throw new UnknownException(message);

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.wildbit.java.postmark.client.data.model.PostmarkError;

import java.io.IOException;

Expand Down Expand Up @@ -59,34 +60,6 @@ public <T> T fromJson(String response, TypeReference<T> valueType) throws IOExce
return this.mapper.readValue(response, valueType);
}

/**
* Helper for filtering out message only returned by HTTP client
* @param data JSON object as String
* @return message
* @throws IOException in case converting String to Object fails
*/
public String formatErrorMessage(String data) throws IOException {
JsonNode node = fromJson(data, JsonNode.class);
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
@@ -0,0 +1,27 @@
package com.wildbit.java.postmark.client.data.model;

/**
* Postmark standard error.
*/
public class PostmarkError {

private Integer errorCode;
private String message;

public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getErrorCode() {

return errorCode;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.wildbit.java.postmark.client.exception;

import com.wildbit.java.postmark.Postmark;
import com.wildbit.java.postmark.client.data.model.PostmarkError;

/**
* Group of classes that identifies main Postmark API exceptions.
*/
public class InvalidAPIKeyException extends PostmarkException {
public InvalidAPIKeyException(String message) {
super(message);
public InvalidAPIKeyException(PostmarkError error) {
super(error.getMessage(), error.getErrorCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.wildbit.java.postmark.client.exception;

import com.wildbit.java.postmark.client.data.model.PostmarkError;

/**
* Group of classes that identifies main Postmark API exceptions.
*/

public class InvalidMessageException extends PostmarkException {
private final Integer errorCode;

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

public Integer getErrorCode() {
return errorCode;
public InvalidMessageException(PostmarkError error) {
super(error.getMessage(), error.getErrorCode());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
*/

public class PostmarkException extends Exception {
private Integer errorCode;

public PostmarkException(String message) {
super(message);
}

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

public Integer getErrorCode() {
return errorCode;
}
}

This file was deleted.

3 changes: 1 addition & 2 deletions src/test/java/integration/MessagesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void messageById() throws PostmarkException, IOException {
void messageByIdWithParameters() throws PostmarkException, IOException {
OutboundMessages messages = client.getMessages(Parameters.init().build("count", 1).build("offset", 0));
String messageId = messages.getMessages().get(0).getMessageId();
Parameters parameters = new Parameters()
.build("includeMessageContent", "Full");
Parameters parameters = new Parameters().build("includeMessageContent", "Full");
OutboundMessageDetails message = client.getMessageDetails(messageId, parameters);

assertNotNull(message.getReceivedAt());
Expand Down
9 changes: 0 additions & 9 deletions src/test/java/unit/client/data/DataHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,4 @@ void fromJsonTypeReference() throws IOException {
assertEquals(result,getHashMap());
}

@Test
void formatErrorMessage() throws IOException {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("ErrorCode", "Hey test");
hashMap.put("Message", "This is a message");

assertEquals(hashMap.get("Message"),dataHandler.formatErrorMessage(dataHandler.toJson(hashMap)));
}

}

0 comments on commit 4e66d04

Please sign in to comment.