Last active
May 10, 2017 20:45
-
-
Save abdulbasitkay/bbf5695f99a0410c022608fd889b0a5f to your computer and use it in GitHub Desktop.
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
@Provider | |
public class ApiExceptionMapper implements ExceptionMapper<ApiException> { | |
@Override | |
public Response toResponse(ApiException ex) { | |
ErrorEntity error = new ErrorEntity(); | |
error.setStatus(ex.getStatus()); | |
error.setMessage(ex.getMessage()); | |
error.setDeveloperMessage(ex.getMessage()); | |
return Response.status(ex.getStatus()) | |
.entity(error) | |
.type(MediaType.APPLICATION_JSON) | |
.build(); | |
} | |
} |
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
@Provider | |
public class GenericExceptionMapper implements ExceptionMapper<Throwable> { | |
@Override | |
public Response toResponse(Throwable ex) { | |
ErrorEntity error = new ErrorEntity(); | |
setHttpStatus(ex, error); | |
error.setMessage(ex.getMessage()); | |
StringWriter errorStackTrace = new StringWriter(); | |
ex.printStackTrace(new PrintWriter(errorStackTrace)); | |
error.setDeveloperMessage(errorStackTrace.toString()); | |
return Response.status(error.getStatus()) | |
.entity(error) | |
.type(MediaType.APPLICATION_JSON) | |
.build(); | |
} | |
private void setHttpStatus(Throwable ex, ErrorEntity error) { | |
if(ex instanceof WebApplicationException) { | |
error.setStatus(( (WebApplicationException) ex).getResponse().getStatus()); | |
} else { | |
error.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment