| | |
| | | package com.panzhihua.common.exceptions; |
| | | |
| | | import com.panzhihua.common.constants.HttpStatus; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import java.util.List; |
| | | |
| | | import org.springframework.core.Ordered; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.validation.BindingResult; |
| | |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | |
| | | import java.util.List; |
| | | import com.panzhihua.common.constants.HttpStatus; |
| | | import com.panzhihua.common.model.vos.R; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | |
| | | public class GlobalExceptionCapture { |
| | | /** |
| | | * 拦截捕捉自定义异常 TokenException.class |
| | | * @param ex token 异常 |
| | | * |
| | | * @param ex |
| | | * token 异常 |
| | | * @return R 401 |
| | | */ |
| | | @ExceptionHandler(value = TokenException.class) |
| | | public R myErrorHandlerTokenException(TokenException ex) { |
| | | return R.fail(ex.getCode(),ex.getMsg()); |
| | | return R.fail(ex.getCode(), ex.getMsg()); |
| | | } |
| | | |
| | | /** |
| | | * 拦截捕捉自定义异常 PartyBuildingMemberException |
| | | * |
| | | * @param ex |
| | | * 党建异常 |
| | | * @return 异常信息 |
| | | */ |
| | | @ExceptionHandler(value = PartyBuildingMemberException.class) |
| | | public R PartyBuildingMemberException(PartyBuildingMemberException ex) { |
| | | return R.fail(ex.getMsg()); |
| | | } |
| | | |
| | | /** |
| | | * 拦截捕捉自定义异常 UnAuthenticationException.class |
| | | * @param ex 认证 异常 |
| | | * |
| | | * @param ex |
| | | * 认证 异常 |
| | | * @return R 401 |
| | | */ |
| | | @ExceptionHandler(value = UnAuthenticationException.class) |
| | | public R myErrorHandlerUnAuthenticationException(UnAuthenticationException ex) { |
| | | return R.fail(ex.getCode(),"没有登录"); |
| | | return R.fail(ex.getCode(), "没有登录"); |
| | | } |
| | | |
| | | /** |
| | | * 拦截捕捉自定义异常 UnAuthorizationException.class |
| | | * @param ex 权限 异常 |
| | | * |
| | | * @param ex |
| | | * 权限 异常 |
| | | * @return R 403 |
| | | */ |
| | | @ExceptionHandler(value = UnAuthorizationException.class) |
| | | public R myErrorHandlerUnAuthorizationException(UnAuthenticationException ex) { |
| | | return R.fail(ex.getCode(),"没有访问权限"); |
| | | return R.fail(ex.getCode(), "没有访问权限"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * @param ex 数据库异常 |
| | | * @param ex |
| | | * 数据库异常 |
| | | * @return |
| | | */ |
| | | @ExceptionHandler(value = java.sql.SQLSyntaxErrorException.class) |
| | | public R sqlSyntaxExcetption(Exception ex) { |
| | | log.error("数据库异常【{}】",ex.getMessage()); |
| | | log.error("数据库异常【{}】", ex.getMessage()); |
| | | return R.fail("sql语法错误"); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param ex 数据库异常 |
| | | * @param ex |
| | | * 数据库异常 |
| | | * @return |
| | | */ |
| | | @ExceptionHandler(value = java.sql.SQLException.class) |
| | | public R sqlException(Exception ex) { |
| | | log.error("数据库异常【{}】",ex.getMessage()); |
| | | log.error("数据库异常【{}】", ex.getMessage()); |
| | | return R.fail("sql错误"); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param ex 数据库异常 |
| | | * @param ex |
| | | * 服务层业务异常 |
| | | * @return |
| | | */ |
| | | @ExceptionHandler(value = ServiceException.class) |
| | | public R serviceException(Exception ex) { |
| | | log.error("服务层业务异常【{}】",ex.getMessage()); |
| | | public R serviceException(ServiceException ex) { |
| | | log.error("服务层业务异常【{}】", ex.getMessage()); |
| | | return R.fail(ex.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 校验异常 |
| | | * @param ex valid |
| | | * |
| | | * @param ex |
| | | * valid |
| | | * @return 返回json |
| | | */ |
| | | @ExceptionHandler(value = MethodArgumentNotValidException.class) |
| | |
| | | StringBuilder errorMessage = new StringBuilder(); |
| | | if (result.hasErrors()) { |
| | | List<ObjectError> errors = result.getAllErrors(); |
| | | errors.forEach(p ->{ |
| | | FieldError fieldError = (FieldError) p; |
| | | errors.forEach(p -> { |
| | | FieldError fieldError = (FieldError)p; |
| | | errorMessage.append(fieldError.getDefaultMessage()); |
| | | errorMessage.append(" "); |
| | | }); |
| | | } |
| | | return R.fail(HttpStatus.BAD_REQUEST,errorMessage.toString()); |
| | | return R.fail(HttpStatus.BAD_REQUEST, errorMessage.toString()); |
| | | } |
| | | |
| | | // /** |
| | | // * 全局异常捕捉处理 |
| | | // * @param ex 所有运行时异常 |
| | | // * @return R 500 |
| | | // */ |
| | | // @ExceptionHandler(value = Exception.class) |
| | | // public R errorHandler(Exception ex) { |
| | | // log.error("捕捉到全局异常【{}】",ex.getMessage()); |
| | | // return R.fail("接口请求失败"); |
| | | // } |
| | | // /** |
| | | // * 全局异常捕捉处理 |
| | | // * @param ex 所有运行时异常 |
| | | // * @return R 500 |
| | | // */ |
| | | // @ExceptionHandler(value = Exception.class) |
| | | // public R errorHandler(Exception ex) { |
| | | // log.error("捕捉到全局异常【{}】",ex.getMessage()); |
| | | // return R.fail("接口请求失败"); |
| | | // } |
| | | } |