@Provider
public class RestfulObjectsApplicationExceptionMapper implements ExceptionMapper<RestfulObjectsApplicationException> {
@Override
public Response toResponse(final RestfulObjectsApplicationException ex) {
final ResponseBuilder builder = Response.status(ex.getHttpStatusCode().getJaxrsStatusType());
// body and content-type
final JsonRepresentation bodyRepr = ex.getBody();
final Throwable cause = ex.getCause();
if (bodyRepr != null) {
final String body = bodyRepr.toString();
builder.entity(body);
builder.type(MediaType.APPLICATION_JSON); // generic; the spec doesn't define what the media type should be
} else if(cause == null) {
builder.type(MediaType.APPLICATION_JSON); // generic; the spec doesn't define what the media type should be
} else {
String body;
try {
body = JsonMapper.instance().write(ExceptionPojo.create(cause));
} catch (final Exception e) {
// fallback
body = "{ \"exception\": \"" + ExceptionUtils.getFullStackTrace(cause) + "\" }";
}
builder.entity(body);
builder.type(RestfulMediaType.APPLICATION_JSON_ERROR);
}
final String message = ex.getMessage();
if (message != null) {
builder.header(RestfulResponse.Header.WARNING.getName(), RestfulResponse.Header.WARNING.render(message));
}
return builder.build();
}