{
if (e instanceof WebApplicationException)
{
Response errorResponse = ((WebApplicationException)e).getResponse();
ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(WebApplicationException.class);
// should be some of 4xx status
if (errorResponse.getStatus() < 500)
{
if (LOG.isDebugEnabled() && e.getCause() != null)
{
LOG.warn("WebApplication exception occurs.", e.getCause());
}
if (errorResponse.getEntity() == null)
{
if (excmap != null)
{
errorResponse = excmap.toResponse(e);
}
}
if (e.getMessage() != null)
errorResponse =
Response.status(errorResponse.getStatus()).entity(new String(e.getMessage())).type(
MediaType.TEXT_PLAIN).header("JAXRS-Message-Provided", "true").build();
response.setResponse(errorResponse);
}
else
{
if (errorResponse.getEntity() == null)
{
if (excmap != null)
{
if (LOG.isDebugEnabled() && e.getCause() != null)
{
// Hide error message if exception mapper exists.
LOG.warn("WebApplication exception occurs.", e.getCause());
}
errorResponse = excmap.toResponse(e);
}
else
{
if (e.getCause() != null)
{
LOG.warn("WebApplication exception occurs.", e.getCause());
}
// print stack trace & adding ex message into body
if (LOG.isDebugEnabled())
{
e.printStackTrace();
}
if (e.getMessage() != null)
errorResponse =
Response.status(errorResponse.getStatus()).entity(new String(e.getMessage())).type(
MediaType.TEXT_PLAIN).header("JAXRS-Message-Provided", "true").build();
else
errorResponse = Response.status(errorResponse.getStatus()).header("JAXRS-Message-Provided", "false").build();
}
}
response.setResponse(errorResponse);
}
}
else if (e instanceof InternalException)
{
Throwable cause = e.getCause();
Class causeClazz = cause.getClass();
ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
while (causeClazz != null && excmap == null)
{
excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
if (excmap == null)
causeClazz = causeClazz.getSuperclass();
}
if (excmap != null)
{
if (LOG.isDebugEnabled())
{
// Hide error message if exception mapper exists.
LOG.warn("Internal error occurs.", cause);
}
response.setResponse(excmap.toResponse(e.getCause()));
}
else
{
LOG.error("Internal error occurs.", cause);
throw new UnhandledException(e.getCause());