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:
- HTTP 200 OK indicates successful response, no errors at all whatsoever; and
- HTTP 404 indicates client-side error; and
- HTTP 500 indicates server-side error
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:
- Will this
ExceptionMapper
really catch allThrowables
(everyException
andError
subclass), or justThrowables
?; and - What can I do inside
isClientSide(...)
to determine whether the error thrown was client- or server-side in origin? Let's pretend that aWebApplicationException
created with a status ofFORBIDDEN
should be considered "client-side", but one created with a status ofBAD_GATEWAY
should not be.