Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard : Exception Handling, giving custom error JSON error messages to client

How can i configure dropwizard to give custom error messages to User. If my function is supposed to return a object myObject, but since there is an error, it should throw and exception and return a error Object to User with a custom message.

like image 756
Dheerendra Avatar asked May 13 '14 10:05

Dheerendra


Video Answer


1 Answers

You can add your own subclass of WebApplicationException like this:

public class ObjectNotFoundException extends WebApplicationException {

  public ObjectNotFoundException() {
    super(Responses.notFound().build());
  }

  public ObjectNotFoundException(String message) {
    super(Response.status(Responses.NOT_FOUND).
    entity(message).type("text/plain").build());
  }

}

See the documentation for more information. You can also use ExceptionMappers if that makes more sense for your application.

like image 62
condit Avatar answered Oct 12 '22 18:10

condit