Result對象 + 統一異常處理

1. 錯誤異常碼設計

1.1 統一異常碼接口定義

/**
 * 統一異常碼接口定義
 *
 * @author 王洪玉
 * @date 2018/11/11
 */

public interface ExceptionEnum {

    /**
     * 獲取異常編碼
     *
     * @return 異常碼
     */
    Integer getCode();

    /**
     * 獲取異常信息
     *
     * @return 異常信息
     */
    String getMessage();
}

1.2. 通用異常錯誤碼Enum

/**
 * 全局異常錯誤碼
 */
public enum ResultMsgEnum implements ExceptionEnum {

    // 請求成功
    SUCCESS(200,"成功"),
    // 服務器內部錯誤
    ERROR(500,"失敗");

    private int code;
    private String message;

    ResultMsgEnum(int value, String text) {
        this.code = value;
        this.message = text;
    }

    @Override
    public Integer getCode() {
        return code;
    }

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

1.3. 業務異常錯誤碼Enum

/**
 * CMS系統錯誤異常碼 5001**
 */
public enum CmsErrorCodeEnum implements ExceptionEnum {
    // 文章錯誤
    Article_NOT_EXIST(500100,"該文章不存在");


    CmsErrorCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private Integer code;
    private String message;

    @Override
    public Integer getCode() {
        return code;
    }

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

2. Result對象設計

/**
 * 通用返回result
 */
@Data
public class ZingResult<T> implements Serializable {

    private int code;
    private String msg;
    private T data;


    private ZingResult() {
        this.code = ResultMsgEnum.SUCCESS.getCode();
        this.msg = ResultMsgEnum.SUCCESS.getMessage();
    }

    private ZingResult(T data) {
        this.code = ResultMsgEnum.SUCCESS.getCode();
        this.msg = ResultMsgEnum.SUCCESS.getMessage();
        this.data = data;
    }


    private ZingResult(ExceptionEnum exceptionEnum) {
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMessage();
    }

    public static ZingResult success() {
        return new ZingResult();
    }

    public static <T> ZingResult<T> success(T data) {
        return new ZingResult<>(data);
    }

    public static <T> ZingResult<T> error(ExceptionEnum exceptionEnum) {
        return new ZingResult<>(exceptionEnum);
    }

}

3. 統一異常處理

3.1 異常父類

/**
 * 異常父類
 *
 * @author 王洪玉
 * @date 2018/11/11
 */

@Getter
public class ZingException extends RuntimeException {

    private Integer code;
    private String message;

    public ZingException(ExceptionEnum exceptionEnum){
        super(exceptionEnum.getMessage());
        this.code = exceptionEnum.getCode();
        this.message = exceptionEnum.getMessage();
    }
}

3.2 業務異常類

/**
 * 業務自定義異常類
 */
@Getter
public class BusinessException extends ZingException {
    private ExceptionEnum exceptionEnum;

    public BusinessException(ExceptionEnum exceptionEnum) {
        super(exceptionEnum);
        this.exceptionEnum = exceptionEnum;
    }
}

3.3 異常攔截處理

@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 攔截未知錯誤異常
     *
     * @param request 請求
     * @param e       未知異常
     * @return 通用返回格式
     */
    @ExceptionHandler(Exception.class)
    public ZingResult cmsException(HttpServletRequest request, Exception e) {
        log.error("請求的url爲{}出現系統異常,異常信息爲:", request.getRequestURI(), e);
        return ZingResult.error(ResultMsgEnum.ERROR);
    }


    /**
     * 攔截CMS業務異常
     *
     * @param request 請求
     * @param e       業務異常
     * @return 通用返回格式
     */
    @ExceptionHandler(CmsBusinessException.class)
    public ZingResult cmsBusinessException(HttpServletRequest request, CmsBusinessException e) {
        log.error("請求的url爲{}出現業務異常,異常信息爲:", request.getRequestURI(), e);
        return ZingResult.error(e.getExceptionEnum());
    }
}

4. 使用示例

if(CollectionUtils.isEmpty(articleList)){
    throw new CmsBusinessException(CmsErrorCodeEnum.ARTICLE_NOT_EXIST);
}
相關文章
相關標籤/搜索