1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package com.fdkankan.common.exception;
- import com.fdkankan.common.model.Result;
- import com.fdkankan.common.model.ViewResult;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.support.DefaultMessageSourceResolvable;
- import org.springframework.http.HttpStatus;
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.MissingServletRequestParameterException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import javax.servlet.http.HttpServletRequest;
- import java.util.stream.Collectors;
- /**
- * 全局异常捕获统一返回客户端
- * @author Admin
- */
- @Slf4j
- @RestControllerAdvice
- public class GlobalExceptionHandler {
- /**
- * 处理其他异常
- * @param e
- * @return
- */
- @ExceptionHandler(value = Exception.class)
- public ViewResult exceptionHandler(Exception e){
- log.error("程序错误原因:", e);
- return ViewResult.error("服务端异常");
- }
- /**
- * 处理其他异常
- * @param e
- * @return
- */
- @ExceptionHandler(value = MissingServletRequestParameterException.class)
- public ViewResult exceptionHandler(MissingServletRequestParameterException e){
- log.error("缺少必要参数:", e);
- return ViewResult.error(HttpStatus.BAD_REQUEST.value(), "缺少必要参数");
- }
- /**
- * 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常
- */
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public ViewResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
- String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
- //下边ResultCodeEnum.PARAMS_BS_ERROR.getCode()就是你自己自定义的返回code码
- return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
- }
- /**
- * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常
- */
- @ExceptionHandler(BindException.class)
- public ViewResult BindExceptionHandler(BindException e) {
- String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
- return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
- }
- /**
- * @link https://www.cnblogs.com/cjyboy/p/11465876.html
- * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是ConstraintViolationException
- */
- // @ExceptionHandler(ConstraintViolationException.class)
- // public ViewResult ConstraintViolationExceptionHandler(ConstraintViolationException e) {
- // String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
- // return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
- // }
- @ExceptionHandler(BaseRuntimeException.class)
- @ResponseStatus(HttpStatus.OK)
- public Result runtimeExceptionHandler(HttpServletRequest request, BaseRuntimeException e) {
- log.error("uri:{},exception:{}",request.getRequestURI(),e);
- return Result.failure(e.getCode() == null ? Result.CODE_FAILURE : e.getCode(), e.getMsg());
- }
- }
|