全局异常处理

在写代码中一般都会在controller中写try…catch代码块拦截异常,给前端以统一的返回格式,为了方便,我们可以创建一个全局的异常处理类,帮助我们实现try…catch的功能,自动拦截异常。

1.新增异常类

创意异常类BusinessException,继承RuntimeException ,方便手动抛出异常问题;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import yd.auth.common.ResponseParameter;

@Data
@EqualsAndHashCode(callSuper = true)
public class BusinessException extends RuntimeException {
	
	// 抛出异常自定义状态码
    private String errorCode = ResponseParameter.CODE_HTTP_XXX;

    public BusinessException(Throwable e) {
        super(ExceptionUtil.getMessage(e), e);
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(String message, String code) {
        super(message);
        this.errorCode = code;
    }

    public BusinessException(String messageTemplate, Object... params) {
        super(StrUtil.format(messageTemplate, params));
    }

    public BusinessException(String message, Throwable throwable) {
        super(message, throwable);
    }

    public BusinessException(Throwable throwable, String messageTemplate, Object... params) {
        super(StrUtil.format(messageTemplate, params), throwable);
    }
}

2.新增全局异常处理器

新增全局异常处理器,处理全部异常和自定义异常,Response为统一请求返回类;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import yd.auth.common.Response;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理 Exception 异常
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Response exceptionHandler(Exception e) {
        log.error("服务错误:" + e);
        return Response.failed(e.getMessage());
    }

    /**
     * 处理 BusinessException 异常
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public Response businessExceptionHandler(BusinessException e) {
        log.info("业务异常:{}", e.getMessage());
        return Response.success(e.getMessage()).code(e.getErrorCode());
    }

}

3.统一请求返回类

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 请求返回类
 */
@Data
@ApiModel(description = "请求返回类")
public class Response implements Serializable {

    @ApiModelProperty(value = "返回状态码", required = true)
    private String code = ResponseParameter.CODE_SUCCESS;

    @ApiModelProperty(value = "返回状态值", required = true)
    private String status = ResponseParameter.STATUS_SUCCESS;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回结果:JSON字符串")
    private String result;

    @ApiModelProperty(value = "某些调用接口返回的信息")
    private String jsonDate;

    @ApiModelProperty(value = "返回结果:对象")
    private Object object;


    public static Response success() {
        return new Response().code(ResponseParameter.CODE_SUCCESS).status(ResponseParameter.STATUS_SUCCESS);
    }

    public static Response success(String msg) {
        return Response.success().message(msg);
    }

    public static Response failed() {
        return new Response().code(ResponseParameter.CODE_HTTP_500).status(ResponseParameter.STATUS_FAILED);
    }

    public static Response failed(String msg) {
        return Response.failed().message(msg);
    }

    public Response code(String code) {
        this.setCode(code);
        return this;
    }

    public Response status(String status) {
        this.setStatus(status);
        return this;
    }

    public Response object(Object object) {
        this.setObject(object);
        return this;
    }

    public Response message(String object) {
        this.setMessage(object);
        return this;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值