Search in sources :

Example 1 with ExceptionResponse

use of org.n52.web.exception.ExceptionResponse 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);
    }
}
Also used : ExceptionResponse(org.n52.web.exception.ExceptionResponse) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ExceptionResponse (org.n52.web.exception.ExceptionResponse)1