12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package com.fdkankan.site.exception;
- import cn.dev33.satoken.exception.NotPermissionException;
- import cn.dev33.satoken.exception.SaTokenException;
- import cn.hutool.core.util.NumberUtil;
- import com.dtflys.forest.exceptions.ForestRuntimeException;
- import com.fdkankan.site.common.ResultCode;
- import com.fdkankan.site.common.ResultData;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- /**
- * 全局异常处理器
- */
- @RestControllerAdvice
- @Slf4j
- public class GlobalExceptionHandler {
- /**
- * 处理未知异常
- */
- @ResponseBody
- @ExceptionHandler(value = Exception.class)
- public ResultData exceptionHandler(Exception e) throws Exception {
- log.error("服务错误:", e);
- return ResultData.error( 500, "系统错误");
- }
- @ExceptionHandler(SaTokenException.class)
- public ResultData handlerSaTokenException(SaTokenException e) {
- if( NumberUtil.equals(e.getCode() , 11012)|| NumberUtil.equals(e.getCode() , 11011)) {
- return ResultData.error(ResultCode.USER_NOT_LOGIN);
- }
- return ResultData.error(e.getCode(),e.getMessage());
- }
- /**
- * 处理业务异常
- */
- @ResponseBody
- @ExceptionHandler(value = BusinessException.class)
- public ResultData businessExceptionHandler(BusinessException e) {
- log.error("业务异常code:{},message:{}", e.getCode(), e.getMessage());
- return ResultData.error(e.getCode(), e.getMessage());
- }
- }
|