You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.1 KiB
Java
53 lines
2.1 KiB
Java
package com.glxp.udi.admin.handler;
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
|
import cn.dev33.satoken.exception.NotPermissionException;
|
|
import cn.dev33.satoken.exception.SaTokenException;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
import com.glxp.udi.admin.common.enums.ResultEnum;
|
|
import com.glxp.udi.admin.common.util.ResultVOUtils;
|
|
import com.glxp.udi.admin.exception.JsonException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
/**
|
|
* 错误回调
|
|
*/
|
|
@RestControllerAdvice
|
|
@Slf4j
|
|
public class GlobalExceptionHandler {
|
|
|
|
// 拦截API异常
|
|
@ExceptionHandler(value = JsonException.class)
|
|
public com.glxp.udi.admin.common.res.BaseResponse handlerJsonException(JsonException e) {
|
|
// 返回对应的错误信息
|
|
return ResultVOUtils.error(e.getCode(), e.getMessage());
|
|
}
|
|
|
|
// 拦截API异常
|
|
@ExceptionHandler(value = RuntimeException.class)
|
|
public com.glxp.udi.admin.common.res.BaseResponse handlerRuntimeException(RuntimeException e) {
|
|
log.error(e.getMessage(), e);
|
|
String errorMsg = StrUtil.isBlank(e.getMessage()) ? ResultEnum.NOT_NETWORK.getMessage() : e.getMessage();
|
|
// 返回对应的错误信息
|
|
return ResultVOUtils.error(ResultEnum.NOT_NETWORK, errorMsg);
|
|
}
|
|
|
|
// 拦截API异常
|
|
@ExceptionHandler(value = SaTokenException.class)
|
|
public com.glxp.udi.admin.common.res.BaseResponse handlerSaTokenException(SaTokenException e) {
|
|
log.error(e.getMessage(), e);
|
|
if (e instanceof NotPermissionException) {
|
|
return ResultVOUtils.error(ResultEnum.AUTH_FAILED.getCode(), "没有权限");
|
|
} else if (e instanceof NotLoginException) {
|
|
return ResultVOUtils.error(ResultEnum.LOGIN_VERIFY_FALL);
|
|
}
|
|
String errorMsg = StrUtil.isBlank(e.getMessage()) ? ResultEnum.NOT_NETWORK.getMessage() : e.getMessage();
|
|
// 返回对应的错误信息
|
|
return ResultVOUtils.error(ResultEnum.NOT_NETWORK, errorMsg);
|
|
}
|
|
|
|
}
|