{
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("WedApplication exception occurs.", e.getCause());
}
if (errorResponse.getEntity() == null)
{
if (excmap != null)
{
errorResponse = excmap.toResponse(e);
}
}
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("WedApplication exception occurs.", e.getCause());
}
errorResponse = excmap.toResponse(e);
}
else
{
if (e.getCause() != null)
{
LOG.warn("WedApplication exception occurs.", e.getCause());
}
// add stack trace as message body
errorResponse =
Response.status(errorResponse.getStatus()).entity(new ErrorStreaming(e)).type(
MediaType.TEXT_PLAIN).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());