spring boot自定义异常与统一异常处理

spring boot自定义异常与统一异常处理

1.定义自定义异常类

例:

public class AppException extends RuntimeException {

    private final String code;
    private final String message;

    /**
     * @description 根据异常码、异常信息和异常包装统一异常
     * @params [code, message, throwable]
     * @return
     */
    public AppException(String code, String message, Throwable throwable) {
        super(message, throwable);
        this.code = code;
        this.message = message;
    }

    /**
     * @description 根据自定义异常枚举类包装统一异常
     * @params [exceptionCode]
     * @return
     */
    public AppException(MyExceptionCode exceptionCode) {
        super(exceptionCode.getMessage());
        this.code = exceptionCode.getCode();
        this.message = exceptionCode.getMessage();
    }

    /**
     * @description 根据自定义异常枚举类和异常包装统一异常
     * @params [exceptionCode, throwable]
     * @return
     */
    public AppException(MyExceptionCode exceptionCode, Throwable throwable) {
        super(exceptionCode.getMessage(), throwable);
        this.code = exceptionCode.getCode();
        this.message = exceptionCode.getMessage();
    }

    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

例:

public class BusinessException extends AppException {

    /**
     * @description 根据异常码、异常信息和异常包装统一异常
     * @params [code, message, throwable]
     * @return
     */
    public BusinessException(String code, String message, Throwable throwable) {
        super(code, message, throwable);
    }

    /**
     * @description 根据自定义异常枚举类包装统一异常
     * @params [exceptionCode]
     * @return
     */
    public BusinessException(MyExceptionCode exceptionCode) {
        super(exceptionCode);
    }

    /**
     * @description 根据自定义异常枚举类和异常包装统一异常
     * @params [exceptionCode, throwable]
     * @return
     */
    public BusinessException(MyExceptionCode exceptionCode, Throwable throwable) {
        super(exceptionCode, throwable);
    }

}

2.定义异常码

例:

@Getter
@AllArgsConstructor
public enum SystemCode implements MyExceptionCode {

    /**
     * SYSTEM_SUCCESS
     */
    SYSTEM_SUCCESS("0", "成功."),

    /**
     * XX_EXCEPTION
     */
    XX_EXCEPTION("100101", "XX_EXCEPTION.")
    ;

    private String code;
    private String message;

}

3.定义全局异常捕获类

例:

@Slf4j
@ControllerAdvice
public class GlobalExceptionResolver {

    public static final String ERROR_STRING = "{}:code:{} # message:{}";

    /**
     * @description 处理自定义异常AppException
     * @params [e]
     * @return boss.xtrain.log.exception.common.CommonResponse
     */
    @ExceptionHandler(value = AppException.class)
    @ResponseBody
    public CommonResponse handleAppException(AppException e) {
        String type = "APP异常";
        log.error(ERROR_STRING, type, e.getCode(), e.getMessage(), e);
        return CommonResponse.error(e.getMessage(), e.getCode());
    }

    /**
     * @description 处理参数校验异常MethodArgumentNotValidException
     * @params [e]
     * @return boss.xtrain.log.exception.common.CommonResponse
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public CommonResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String type = "参数校验异常";
        StringBuilder message = new StringBuilder();
        for (FieldError error : e.getBindingResult().getFieldErrors()) {
            message.append(error.getDefaultMessage()).append(";");
        }
        log.error(ERROR_STRING, type, "150101", e.getMessage(), e);
        return CommonResponse.error(message.toString(), "150101");
    }

    /**
     * @description 处理服务调用异常Exception
     * @params [e]
     * @return boss.xtrain.log.exception.common.CommonResponse
     */
    @ExceptionHandler(value = ClientException.class)
    @ResponseBody
    public CommonResponse handleClientException(ClientException e) {
        String type = "服务间调用异常";
        log.error(ERROR_STRING, type, "150102", e.toString(), e);
        return CommonResponse.error(type, "150102");
    }

}

4.使用

例:

    try {
        row = paperDao.save(paperEntity);
    } catch (Exception e) {
        throw new BusinessException(SystemCode.XX_EXCEPTION, e);
    }
posted @ 2020-10-08 17:17  豫若涉川  阅读(399)  评论(0编辑  收藏  举报