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