Search in sources :

Example 81 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 82 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project spring-security-oauth by spring-projects.

the class CheckTokenEndpoint method handleException.

@ExceptionHandler(InvalidTokenException.class)
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
    logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
    // This isn't an oauth resource, so we don't want to send an
    // unauthorized code here. The client has already authenticated
    // successfully with basic auth and should just
    // get back the invalid token error.
    @SuppressWarnings("serial") InvalidTokenException e400 = new InvalidTokenException(e.getMessage()) {

        @Override
        public int getHttpErrorCode() {
            return 400;
        }
    };
    return exceptionTranslator.translate(e400);
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 83 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project springside4 by springside.

the class CustomExceptionHandler method handleServiceException.

@ExceptionHandler(value = { ServiceException.class })
public final ResponseEntity<ErrorResult> handleServiceException(ServiceException ex, HttpServletRequest request) {
    // 注入servletRequest,用于出错时打印请求URL与来源地址
    logError(ex, request);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(MediaTypes.JSON_UTF_8));
    ErrorResult result = new ErrorResult(ex.errorCode.code, ex.getMessage());
    return new ResponseEntity<ErrorResult>(result, headers, HttpStatus.valueOf(ex.errorCode.httpStatus));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 84 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project topjava10 by JavaWebinar.

the class GlobalControllerExceptionHandler method defaultErrorHandler.

@ExceptionHandler(Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
    LOG.error("Exception at request " + req.getRequestURL(), e);
    ModelAndView mav = new ModelAndView("exception/exception");
    mav.addObject("exception", e);
    // Interceptor is not invoked, put userTo
    AuthorizedUser authorizedUser = AuthorizedUser.safeGet();
    if (authorizedUser != null) {
        mav.addObject("userTo", authorizedUser.getUserTo());
    }
    return mav;
}
Also used : AuthorizedUser(ru.javawebinar.topjava.AuthorizedUser) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 85 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler 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

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