-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated error handling to provide http status on exception
- Loading branch information
Showing
8 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/postmarkapp/postmark/client/exception/PostmarkHttpException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |