-
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.
Added error code to error messages that have error code. Separated er…
…ror handling from http client handler. Added parameter usage to message details endpoint
- Loading branch information
Showing
12 changed files
with
118 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
**/*.idea | ||
**/*.iml | ||
target | ||
out | ||
src/test/java/temp | ||
src/test/resources/*properties* | ||
|
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/wildbit/java/postmark/client/HttpClientErrorHandler.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,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); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/wildbit/java/postmark/client/data/model/PostmarkError.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,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; | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
src/main/java/com/wildbit/java/postmark/client/exception/InvalidAPIKeyException.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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
13 changes: 4 additions & 9 deletions
13
src/main/java/com/wildbit/java/postmark/client/exception/InvalidMessageException.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
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
12 changes: 0 additions & 12 deletions
12
src/main/java/com/wildbit/java/postmark/client/exception/UnknownMessageType.java
This file was deleted.
Oops, something went wrong.
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