Search in sources :

Example 1 with WebException

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);
    }
}
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)

Example 2 with WebException

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);
    }
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) WebException(org.n52.web.exception.WebException) InternalServerException(org.n52.web.exception.InternalServerException) BadRequestException(org.n52.web.exception.BadRequestException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

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 BadRequestException (org.n52.web.exception.BadRequestException)1 ExceptionResponse (org.n52.web.exception.ExceptionResponse)1 InternalServerException (org.n52.web.exception.InternalServerException)1 WebException (org.n52.web.exception.WebException)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1