guiceguice-3

ExceptionHandler equivalent in Guice


I'm having Controller which can throw my Custom Exception.

public Response fetch(final String id) throws MyException {
    return Response.ok().entity(custonService.fetch(id)).build();
}

I want to handle that exception and set response status as bad request with custom message as it happen in spring with the help of ExceptionHandler. I saw CheckedProvider but couldn't find how to set response status and message in case of exception.
Is there a way to do so?
I'm new to Guice, please help.


Solution

  • It's not a Guice thing. Guice is just a dependency injection framework, it doesn't have anything to do with REST APIs. It looks like you are using JAX-RS, which means to control the status code and body of an error response, you need to use the ExceptionMapper system provided by JAX-RS.

    On a separate note, you probably shouldn't be using a Response object. Just return the value and it will automatically get a 200 OK status code.