I am wondering what the proper use is for the Javax-RS/Jersey ExceptionMapper class when it comes to mapping WebApplicationExceptions
I like the following, simple, 3-status paradigm:
The problem with WebApplicationExceptions is that they could either be client-side (403, Forbidden) or server-side (503 Bad Gateway); hence they might map to a 404 or 500 depending on the situation.
I'm struggling with trying to inspect WebApplicationException so that I can determine whether its client- or server-side.
My best attempt thus far:
// Groovy pseudo-code
class MyMapper implements ExceptionMapper<Throwable> {
@Override
Response toResponse(Throwable error) {
if(error instanceof WebApplicationException) {
if(isClientSide(error as WebApplicationException)) {
// Return HTTP 404.
} else {
// Return HTTP 500.
}
} else if(error instanceof ClientException) {
// Return HTTP 404.
} else if(error instanceof ServerException) {
// Return HTTP 500.
} else {
// All other throwables. Default to HTTP 500.
}
}
private boolean isClientSide(WebApplicationException webAppExc) {
// TODO: How to make this determination?
}
}
So a few concerns/issues here:
ExceptionMapper really catch all Throwables (every Exception and Error subclass), or just Throwables?; andisClientSide(...) to determine whether the error thrown was client- or server-side in origin? Let's pretend that a WebApplicationException created with a status of FORBIDDEN should be considered "client-side", but one created with a status of BAD_GATEWAY should not be.WebApplicationException and ExceptionMapper serve similar but slightly different purposes.
They both help the developer in setting a custom HTTP error code and response on occurence of specific exceptions.
WebApplicationException is mostly used for custom or user defined exceptions; which means whenever your application throws a user-defined exception you can set the HTTP response code that you want with a detailed description of what the problem is and also set the return type.
Looks something like this:-
public class UserDefinedExcpetion extends WebApplicationException {
/**
* Create a HTTP 404 Not Found Error as plain text
* whenever a UserDefinedExcpetion happens.
*/
public UserDefinedExcpetion (String yourMessage) {
super(Response.status(Responses.NOT_FOUND).
entity(yourMessage).type("text/plain").build());
}
}
You use a ExceptionMapper on exceptions that already exist(for example ones not defined by the user) in your application and on occurance of these exceptions you want to send a custom HTTP error response with some details embedded; and setting the type of the message.
Use it like this:-
@Provider
public class IOExceptionMapper implements ExceptionMapper<java.io.IOException> {
public Response toResponse(java.io.IOException) {
/** Mapper which maps to IOExcpetion and gets called automatically by the JAXRS runtime when this exception occurs. Throw a 404 when this exception occurs.**/
return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}
}
NOTE:- Note the @Provider annotation which registers this mapper in jaxrs runtime.
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