Skip to content

Instantly share code, notes, and snippets.

@leonmaia
Created May 11, 2016 13:02
Show Gist options
  • Save leonmaia/852387c82ae883cc85387e83fa516733 to your computer and use it in GitHub Desktop.
Save leonmaia/852387c82ae883cc85387e83fa516733 to your computer and use it in GitHub Desktop.
public final class HandleErrors extends SimpleFilter<Request, Response> {
@Override
public Future<Response> apply(Request req, Service<Request, Response> service) {
return service.apply(req).handle(new ExceptionalFunction<Throwable, Response>() {
@Override
public Response applyE(Throwable in) {
Response resp = Response.apply();
ChannelBuffer content = ChannelBuffers.wrappedBuffer(in.getMessage().getBytes());
if (in instanceof NumberFormatException) {
resp.setStatus(HttpResponseStatus.BAD_REQUEST);
resp.setContent(content);
return resp;
}
in.printStackTrace();
resp.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
resp.setContent(content);
return resp;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment