class ThrowableExceptionMapper implements ExceptionMapper<Throwable> {

	@Override
	public Response toResponse(Throwable t) {
		if (t instanceof WebApplicationException) {
			WebApplicationException webEx = (WebApplicationException) t;
			if(webEx.getResponse().getStatus() == 500) {
				this.logger.error("Internal server error while handling a request", webEx);
				//Important: WebApplications with different status don't get logged
				//they are used for building responses that are a expected behaviour
				//but have a different http status
			}
			ResponseBuilder rb = Response.status(webEx.getResponse().getStatus());
			String message = webEx.getLocalizedMessage();
			if (webEx.getCause() != null) {
				message = message + " caused by " + webEx.getCause().toString();
			}
			return rb.entity(message).type(MediaType.TEXT_PLAIN).build();
		}
		ResponseBuilder rb = Response.status(Status.INTERNAL_SERVER_ERROR);
		String message = t.toString();
		this.logger.error("Error while HTTP Request on the Server", t);

		return rb.entity(message).type(MediaType.TEXT_PLAIN).build();
	}

}