@RestControllerAdvice全局异常捕捉

该博客介绍了如何在Java应用中自定义异常`EbException`,并创建全局异常捕获器`EbExceptionAdvice`来处理运行时异常。通过`EbErrorCode`枚举定义了错误码和错误消息,确保了统一的错误响应。异常处理涉及用户认证错误、输入缺失等业务场景,并提供详细的错误堆栈信息用于问题排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义异常EbException

public class EbException extends RuntimeException
{
    private final String UNKOWN_ERROR = "unknown.error";

    private String msg;

    private String errorDetail;

    private Integer errorCode = 10000;


    public EbException(String detail)
    {
        super(detail);
        this.errorDetail = detail;
        this.msg= I18nUtil.get(UNKOWN_ERROR);
    }


    public Integer getErrorCode()
    {
        return errorCode;
    }

    public String getMsg()
    {
        return msg;
    }

    public String getDetail()
    {
        return errorDetail;
    }

    public ResponseEntity<BaseResponse> toResponseEntity()
    {
        return ResponseEntity.ok().body(new BaseResponse<>(getErrorCode(), null, getMsg(), errorDetail));
    }
}

全局异常捕捉器

@RestControllerAdvice
@Slf4j
public class EbExceptionAdvice
{
    @ExceptionHandler(EbException.class)
    public final ResponseEntity<BaseResponse> handleKgServiceException(EbException ex, HttpServletRequest request)
    {
        log.error("Catch {} from controller, the request is {} {}", ex.getClass().getName(), request.getRequestURI(),ex);
        // print business-related stacktrace for troubleshooting
        log.debug("Exception trace {}", ex);
        return ex.toResponseEntity();
    }

    @ExceptionHandler(RuntimeException.class)
    public final ResponseEntity<BaseResponse> handleRuntimeException(RuntimeException ex, HttpServletRequest request)
    {
        log.error("Catch {} from controller, the request is {} {}", ex.getClass().getName(), request.getRequestURI(),ex);
        // print business-related stacktrace for troubleshooting
        log.info("Catch runtime exception");
        return ResponseEntity.ok().body(new BaseResponse<>(EbErrorCode.UNKNOWN_ERROR.getErrorCode(),
                null, I18nUtil.get(EbErrorCode.UNKNOWN_ERROR.getErrorMsg()), ex.getMessage()));
    }
}

定义异常码

public enum EbErrorCode
{
    //用户登录错误
    JWT_TOKEN_INVALID(90001, "ebauth.token.invalid"),
    JWT_TOKEN_EXPIRE(90002, "ebauth.token.expire"),
    USER_NOT_EXIST(90003, "ebauth.user.not.exist"),
    USER_DISABLED(90003, "ebauth.user.disabled"),
    TEAM_DISABLED(90004, "ebauth.team.disabled"),
    USER_AUTHORIZATION_ERROR(90005, "ebauth.user.authorization"),
    //

    AD_INPUT_MISSING(90005, "ebadauth.missing.input"),

    UNKNOWN_ERROR(100000, "unknown.error");

    private Integer errorCode;

    private String errorMsg;

    EbErrorCode(Integer errorCode, String errorMsg)
    {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public Integer getErrorCode()
    {
        return this.errorCode;
    }

    public String getErrorMsg() { return this.errorMsg; }
}

原理:被 @ExceptionHandler、@InitBinder、@ModelAttribute 注解的方法,都会作用在 被 @RequestMapping (@GetMapping等注解同理)注解的方法上。

参考:https://www.cnblogs.com/magicalSam/p/7198420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值