Skip to content

Commit

Permalink
updated error handling to provide http status on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Mar 2, 2023
1 parent 65acbc9 commit a53eabb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public HttpClientErrorHandler(DataHandler dataHandler) {
*
* @throws java.io.IOException in case invalid HTTP response is returned.
*/
public PostmarkException throwErrorBasedOnStatusCode(Integer statusCode, String message) throws IOException {
public PostmarkHttpException throwErrorBasedOnStatusCode(Integer statusCode, String message) throws IOException {
switch (statusCode) {
case 401:
return new InvalidAPIKeyException(postmarkErrorFromResponse(message));
return new InvalidAPIKeyException(postmarkErrorFromResponse(message), statusCode);

case 408:
return new TimeoutException(message);
return new TimeoutException(message, statusCode);

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

case 500:
return new InternalServerException(message);
return new InternalServerException(message, statusCode);

default:
return new UnknownException(message);
return new UnknownException(message, statusCode);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Group of classes that identifies main Postmark API exceptions.
*/

public class InternalServerException extends PostmarkException {
public InternalServerException(String message) {
super(message);
public class InternalServerException extends PostmarkHttpException {
public InternalServerException(String message, Integer statusCode) {
super(message, statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/**
* Group of classes that identifies main Postmark API exceptions.
*/
public class InvalidAPIKeyException extends PostmarkException {
public InvalidAPIKeyException(PostmarkError error) {
super(error.getMessage(), error.getErrorCode());
public class InvalidAPIKeyException extends PostmarkHttpException {
public InvalidAPIKeyException(PostmarkError error, Integer statusCode) {
super(error.getMessage(), error.getErrorCode(), statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* Group of classes that identifies main Postmark API exceptions.
*/

public class InvalidMessageException extends PostmarkException {
public InvalidMessageException(PostmarkError error) {
super(error.getMessage(), error.getErrorCode());
public class InvalidMessageException extends PostmarkHttpException {
public InvalidMessageException(PostmarkError error, Integer statusCode) {
super(error.getMessage(), error.getErrorCode(), statusCode);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.postmarkapp.postmark.client.exception;

import com.postmarkapp.postmark.client.data.model.PostmarkError;

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

public class PostmarkHttpException extends PostmarkException {
private Integer statusCode;
public Integer getStatusCode() {
return statusCode;
}
public PostmarkHttpException(String message, Integer statusCode) {
super(message);
this.statusCode = statusCode;
}
public PostmarkHttpException(String message, Integer errorCode, Integer statusCode) {
super(message, errorCode);
this.statusCode = statusCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Group of classes that identifies main Postmark API exceptions.
*/

public class TimeoutException extends PostmarkException {
public TimeoutException(String message) {
super(message);
public class TimeoutException extends PostmarkHttpException {
public TimeoutException(String message, Integer statusCode) {
super(message, statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Group of classes that identifies main Postmark API exceptions.
*/

public class UnknownException extends PostmarkException {
public UnknownException(String message) {
super(message);
public class UnknownException extends PostmarkHttpException {
public UnknownException(String message, Integer statusCode) {
super(message, statusCode);
}

}
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/unit/client/HttpClientErrorHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package unit.client;

import com.postmarkapp.postmark.client.HttpClientErrorHandler;
import com.postmarkapp.postmark.client.data.parser.DataHandler;
import com.postmarkapp.postmark.client.exception.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;


public class HttpClientErrorHandlerTest {

String getStringHashMap() {

return "{\n" +
" \"ErrorCode\" : \"10\",\n" +
" \"Message\" : \"ErrorMessage\"\n" +
"}";
}

@Test
void throw401error() throws IOException, PostmarkException {
int statusCode = 401;
HttpClientErrorHandler errorHandler = new HttpClientErrorHandler(new DataHandler(false));
InvalidAPIKeyException exception = (InvalidAPIKeyException) errorHandler.throwErrorBasedOnStatusCode(statusCode, getStringHashMap());

assertEquals(exception.getErrorCode(),10);
assertEquals(exception.getMessage(),"ErrorMessage");
assertEquals(exception.getStatusCode(), statusCode);
}

@Test
void throw408error() throws IOException, PostmarkException {
int statusCode = 408;
HttpClientErrorHandler errorHandler = new HttpClientErrorHandler(new DataHandler(false));
TimeoutException exception = (TimeoutException) errorHandler.throwErrorBasedOnStatusCode(statusCode, "message");

assertEquals(exception.getErrorCode(),null);
assertEquals(exception.getMessage(),"message");
assertEquals(exception.getStatusCode(), statusCode);
}

@Test
void throw422error() throws IOException, PostmarkException {
int statusCode = 422;
HttpClientErrorHandler errorHandler = new HttpClientErrorHandler(new DataHandler(false));
InvalidMessageException exception = (InvalidMessageException) errorHandler.throwErrorBasedOnStatusCode(statusCode, getStringHashMap());

assertEquals(exception.getErrorCode(),10);
assertEquals(exception.getMessage(),"ErrorMessage");
assertEquals(exception.getStatusCode(), statusCode);
}

@Test
void throw500error() throws IOException, PostmarkException {
int statusCode = 500;
HttpClientErrorHandler errorHandler = new HttpClientErrorHandler(new DataHandler(false));
InternalServerException exception = (InternalServerException) errorHandler.throwErrorBasedOnStatusCode(statusCode, "message");

assertEquals(exception.getErrorCode(),null);
assertEquals(exception.getMessage(),"message");
assertEquals(exception.getStatusCode(), statusCode);
}

@Test
void throw501error() throws IOException, PostmarkException {
int statusCode = 501;
HttpClientErrorHandler errorHandler = new HttpClientErrorHandler(new DataHandler(false));
UnknownException exception = (UnknownException) errorHandler.throwErrorBasedOnStatusCode(statusCode, "message");

assertEquals(exception.getErrorCode(),null);
assertEquals(exception.getMessage(),"message");
assertEquals(exception.getStatusCode(), statusCode);
}

}

0 comments on commit a53eabb

Please sign in to comment.