@ControllerAdvice public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ConstraintViolationException.class) public DefaultErrorResult handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
log.error("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e); List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertCVSetToParameterInvalidItemList(e.getConstraintViolations()); return DefaultErrorResult.failure(ResultCode.COMM_PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
}
@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) public DefaultErrorResult handleConstraintViolationException(HttpMessageNotReadableException e, HttpServletRequest request) { log.error("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e); return DefaultErrorResult.failure(ResultCode.COMM_PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST); }
@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(BindException.class) public DefaultErrorResult handleBindException(BindException e, HttpServletRequest request) { log.error("handleBindException start, uri:{}, caused by: ", request.getRequestURI(), e); List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult()); return DefaultErrorResult.failure(ResultCode.COMM_PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
}
@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException.class) public DefaultErrorResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) { log.error("handleMethodArgumentNotValidException start, uri:{}, caused by: ", request.getRequestURI(), e); List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult()); return DefaultErrorResult.failure(ResultCode.COMM_PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
} @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(RuntimeException.class) public DefaultErrorResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { log.error("handleRuntimeException start, uri:{}, caused by: ", request.getRequestURI(), e); return DefaultErrorResult.failure(ResultCode.COMM_SYSTEM_INNER_ERROR, e, HttpStatus.INTERNAL_SERVER_ERROR); }
@ExceptionHandler(BusinessException.class) public ResponseEntity<DefaultErrorResult> handleBusinessException(BusinessException e, HttpServletRequest request) { log.error("handleBusinessException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(), e.getMessage()); DefaultErrorResult defaultErrorResult = DefaultErrorResult.failure(e);
return ResponseEntity.status(defaultErrorResult.getStatus()).body(defaultErrorResult); }
|
文章评论