From 8380d20042ffe57471c0f48b7effb74b6f431227 Mon Sep 17 00:00:00 2001 From: infrasys00 <blackdancers@163.com> Date: 星期二, 22 十二月 2020 18:11:30 +0800 Subject: [PATCH] Merge branch 'master' of http://gitlab.nhys.cdnhxx.com/root/zhihuishequ --- springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/exception/GlobalExceptionCapture.java | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 87 insertions(+), 0 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/exception/GlobalExceptionCapture.java b/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/exception/GlobalExceptionCapture.java new file mode 100644 index 0000000..3ce44d6 --- /dev/null +++ b/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/exception/GlobalExceptionCapture.java @@ -0,0 +1,87 @@ +package com.panzhihua.applets.exception; + +import com.panzhihua.common.constants.HttpStatus; +import com.panzhihua.common.exceptions.TokenException; +import com.panzhihua.common.exceptions.UnAuthenticationException; +import com.panzhihua.common.exceptions.UnAuthorizationException; +import com.panzhihua.common.model.vos.R; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.List; + +/** + * @program: springcloud_k8s_panzhihuazhihuishequ + * @description: 全局异常捕获 + * @author: huang.hongfa weixin hhf9596 qq 959656820 + * @create: 2020-11-25 14:36 + **/ +@Slf4j +@RestControllerAdvice +public class GlobalExceptionCapture { + /** + * 拦截捕捉自定义异常 TokenException.class + * @param ex token 异常 + * @return R 401 + */ + @ExceptionHandler(value = TokenException.class) + public R myErrorHandlerTokenException(TokenException ex) { + return R.fail(ex.getCode(),ex.getMsg()); + } + + /** + * 拦截捕捉自定义异常 UnAuthenticationException.class + * @param ex 认证 异常 + * @return R 401 + */ + @ExceptionHandler(value = UnAuthenticationException.class) + public R myErrorHandlerUnAuthenticationException(UnAuthenticationException ex) { + return R.fail(ex.getCode(),ex.getMsg()); + } + + /** + * 拦截捕捉自定义异常 UnAuthorizationException.class + * @param ex 权限 异常 + * @return R 403 + */ + @ExceptionHandler(value = UnAuthorizationException.class) + public R myErrorHandlerUnAuthorizationException(UnAuthenticationException ex) { + return R.fail(ex.getCode(),ex.getMsg()); + } + +// /** +// * 全局异常捕捉处理 +// * @param ex 所有运行时异常 +// * @return R 500 +// */ +// @ExceptionHandler(value = Exception.class) +// public R errorHandler(Exception ex) { +// log.error("捕捉到全局异常【{}】",ex.getMessage()); +// return R.fail(ex.getMessage()); +// } + + /** + * 校验异常 + * @param ex valid + * @return 返回json + */ + @ExceptionHandler(value = MethodArgumentNotValidException.class) + public R methodArgumentNotValidException(MethodArgumentNotValidException ex) { + BindingResult result = ex.getBindingResult(); + StringBuilder errorMessage = new StringBuilder(); + if (result.hasErrors()) { + List<ObjectError> errors = result.getAllErrors(); + errors.forEach(p ->{ + FieldError fieldError = (FieldError) p; + errorMessage.append(fieldError.getDefaultMessage()); + errorMessage.append(" "); + }); + } + return R.fail(HttpStatus.BAD_REQUEST,errorMessage.toString()); + } +} -- Gitblit v1.7.1