Skip to content

Instantly share code, notes, and snippets.

@abdulbasitkay
Last active May 10, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulbasitkay/bbf5695f99a0410c022608fd889b0a5f to your computer and use it in GitHub Desktop.
Save abdulbasitkay/bbf5695f99a0410c022608fd889b0a5f to your computer and use it in GitHub Desktop.
@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();
}
}
@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