Is it possible to make global exception handler for all unexpected errors. Because it's impossible to make all possible classes like this:
public class ExceptionHandler implements ExceptionMapper<JsonMappingException> {...}
I want something like this:
public class ExceptionHandler implements ExceptionMapper<Exception>
Note that although the accepted answer does catch all exceptions it will cause many other issues, eg interfering with the internal exception handling of Resteasy.
Therefore if you take this approach you should also build a custom exception mapping for each method that is important for you.
In particular this method will break the built in CORS handling, so you should create a CORS custom exception mapper if you allow CORS responses.
@Provider
public class DefaultOptionsExceptionHandler implements ExceptionMapper<DefaultOptionsMethodException> {
@Override
public Response toResponse(DefaultOptionsMethodException e) {
return e.getResponse();
}
}
You could probably do the following:
@Provider
public class GlobalExceptionHandler implements ExceptionMapper<Exception> {
public Response toResponse(Exception exception) {
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("An error occured").type(MediaType.TEXT_PLAIN)
.build();
}
}
I have just tested it in RESTEasy 3.0.6.Final and it seems to work. Keep in mind that the 500 Internal Server Error
status code may not always be appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With