Skip to content

Commit

Permalink
Refactored exception factory to include response id. (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Jan 3, 2018
1 parent 1f44b2d commit cb62c17
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/main/java/com/bunq/sdk/exception/ExceptionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public class ExceptionFactory {
/**
* Some glue to glue the error message together.
*/
private static final String GLUE_ERROR_MESSAGES = "\n";
private static final String SEPARATOR_ERROR_MESSAGES = "\n";

/**
* String format constants.
*/
private static final String FORMAT_ERROR_MESSAGE = "Response id to help bunq debug: %s. \n Error message: %s";

/**
*
Expand All @@ -27,31 +32,31 @@ public class ExceptionFactory {
*
* @return The exception that belongs to this status code.
*/
public static ApiException createExceptionForResponse(int responseCode, List<String> messages){
String error_message = concatenateMessages(messages);
public static ApiException createExceptionForResponse(int responseCode, List<String> messages, String responseId){
String error_message = concatenateMessages(messages, responseId);

switch (responseCode) {
case HTTP_RESPONSE_CODE_BAD_REQUEST:
return new BadRequestException(error_message, responseCode);
return new BadRequestException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_UNAUTHORIZED:
return new UnauthorizedException(error_message, responseCode);
return new UnauthorizedException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_FORBIDDEN:
return new UnauthorizedException(error_message, responseCode);
return new ForbiddenException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_NOT_FOUND:
return new NotFoundException(error_message, responseCode);
return new NotFoundException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED:
return new MethodNotAllowedException(error_message, responseCode);
return new MethodNotAllowedException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS:
return new TooManyRequestsException(error_message, responseCode);
return new TooManyRequestsException(error_message, responseCode, responseId);
case HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR:
return new PleaseContactBunqException(error_message, responseCode);
return new PleaseContactBunqException(error_message, responseCode, responseId);
default:
return new UnknownApiErrorException(error_message, responseCode);
return new UnknownApiErrorException(error_message, responseCode, responseId);
}
}

private static String concatenateMessages(List<String> messages) {
return String.join(GLUE_ERROR_MESSAGES, messages);
private static String concatenateMessages(List<String> messages, String responseId) {
return String.format(FORMAT_ERROR_MESSAGE, responseId, String.join(SEPARATOR_ERROR_MESSAGES, messages));
}

}

0 comments on commit cb62c17

Please sign in to comment.