use of org.n52.web.exception.WebException in project series-rest-api by 52North.
the class BaseController method writeExceptionResponse.
private void writeExceptionResponse(WebException e, HttpServletResponse response, HttpStatus status) {
final String logMessage = "An exception occured.";
if (status == HttpStatus.INTERNAL_SERVER_ERROR) {
LOGGER.error(logMessage, e);
} else {
LOGGER.debug(logMessage, e);
}
// TODO consider using a 'suppress_response_codes=true' parameter and always return 200 OK
response.setStatus(status.value());
response.setContentType(MimeType.APPLICATION_JSON.getMimeType());
ObjectMapper objectMapper = createObjectMapper();
ObjectWriter writer = objectMapper.writerFor(ExceptionResponse.class);
ExceptionResponse exceptionResponse = ExceptionResponse.createExceptionResponse(e, status);
try (OutputStream outputStream = response.getOutputStream()) {
writer.writeValue(outputStream, exceptionResponse);
} catch (IOException ioe) {
LOGGER.error("Could not process error message.", ioe);
}
}
use of org.n52.web.exception.WebException in project series-rest-api by 52North.
the class BaseController method handleException.
@ExceptionHandler(value = { RuntimeException.class, Exception.class, Throwable.class })
public void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
if (e instanceof HttpMessageNotReadableException) {
WebException wrappedException = new BadRequestException("The request could not been read.", e);
wrappedException.addHint("Check the message which has been sent to the server. Probably it is not valid.");
writeExceptionResponse(wrappedException, response, HttpStatus.BAD_REQUEST);
} else {
WebException wrappedException = new InternalServerException("Unexpected Exception occured.", e);
writeExceptionResponse(wrappedException, response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Aggregations