Search in sources :

Example 1 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project rhino by PLOS.

the class RestController method reportClientError.

/**
   * Report an error condition to the REST client. The brief error message is sent as the response body, with the
   * response code specified when the exception object was created. The stack trace is not included because we generally
   * expect the client to fix the error with a simple change to input.
   *
   * @param e the exception that Spring wants to handle
   * @return the RESTful response body
   */
@ExceptionHandler(RestClientException.class)
public ResponseEntity<String> reportClientError(RestClientException e) {
    HttpStatus status = e.getResponseStatus();
    log.info("Reporting error to client (" + status + ")", e);
    String message = e.getMessage().trim() + '\n';
    return respondWithPlainText(message, status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 2 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project rhino by PLOS.

the class RestController method reportServerError.

/**
   * Display a server-side error to the rest client. This is meant generally to handle bugs and configuration errors.
   * Because this is assumed to be caused by programmer error, the stack trace is sent in the request body.
   *
   * @param e the exception that Spring wants to handle
   * @return the RESTful response body
   */
@ExceptionHandler(Exception.class)
public ResponseEntity<String> reportServerError(Exception e) {
    log.error("Exception from controller", e);
    StringWriter report = new StringWriter();
    e.printStackTrace(new PrintWriter(report));
    return respondWithPlainText(report.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 3 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project chassis by Kixeye.

the class HttpExceptionHandler method defaultErrorHandler.

@ExceptionHandler(Exception.class)
@ResponseBody
public ServiceError defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
    ServiceError error = ExceptionServiceErrorMapper.mapException(ex);
    switch(error.code) {
        case ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE:
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            logger.error("Unexpected error", ex);
            break;
        case ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE:
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            if (logger.isDebugEnabled()) {
                logger.debug("Validation exception", ex);
            }
            break;
        case ExceptionServiceErrorMapper.SECURITY_ERROR_CODE:
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            if (logger.isDebugEnabled()) {
                logger.debug("Security exception", ex);
            }
            break;
        default:
            if (ex instanceof HttpServiceException) {
                HttpServiceException httpEx = (HttpServiceException) ex;
                response.setStatus(httpEx.httpResponseCode);
            }
            logger.warn("Service exception", ex);
            break;
    }
    return error;
}
Also used : ServiceError(com.kixeye.chassis.transport.dto.ServiceError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project geode by apache.

the class ExceptionHandlingAdvice method handleExc.

@ExceptionHandler(IOException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleExc(IOException ext) {
    // write errors
    StringWriter swBuffer = new StringWriter();
    PrintWriter prtWriter = new PrintWriter(swBuffer);
    ext.printStackTrace(prtWriter);
    logger.fatal("IOException Details : {}\n", swBuffer);
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 5 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project geode by apache.

the class BaseControllerAdvice method handleException.

/**
   * Handles any Exception thrown by a REST API web service endpoint, HTTP request handler method.
   * <p/>
   * 
   * @param cause the Exception causing the error.
   * @return a ResponseEntity with an appropriate HTTP status code (500 - Internal Server Error) and
   *         HTTP response body containing the stack trace of the Exception.
   */
@ExceptionHandler(Throwable.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(final Throwable cause) {
    final StringWriter stackTraceWriter = new StringWriter();
    cause.printStackTrace(new PrintWriter(stackTraceWriter));
    final String stackTrace = stackTraceWriter.toString();
    if (logger.isDebugEnabled()) {
        logger.debug(stackTrace);
    }
    return convertErrorAsJson(cause.getMessage());
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)178 ResponseEntity (org.springframework.http.ResponseEntity)58 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)48 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)41 HttpHeaders (org.springframework.http.HttpHeaders)39 ModelAndView (org.springframework.web.servlet.ModelAndView)33 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)31 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)17 HttpStatus (org.springframework.http.HttpStatus)13 AjaxJson (com.cdeledu.common.base.AjaxJson)12 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)8 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)8 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)8 RestResponse (org.entando.entando.web.common.model.RestResponse)8 ArrayList (java.util.ArrayList)7 BindingResult (org.springframework.validation.BindingResult)6 FieldError (org.springframework.validation.FieldError)5 ErrorInfo (com.haulmont.restapi.exception.ErrorInfo)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4