[TOC]

1 定义错误码类

能够定义各种错误码枚举,比方业务,零碎相干的报错信息

/** * 错误代码 * 错误码 * * @author leovany * @date 2023/09/23 */public enum ErrorCode {    SUCCESS(0, "success", ""),    ERROR_PARAMS(40000, "申请参数谬误", ""),    ERROR_NULL(40001, "申请数据为空", ""),    ERROR_LOGIN(40100, "未登录", ""),    ERROR_NO_AUTH(41001, "无权限", ""),    ERROR_SYSTEM(50000, "零碎外部异样", "")    ;    /**     * 错误码ID     */    private final int code;    /**     * 错误码信息     */    private final String message;    /**     * 错误码形容(详情)     */    private final String description;    ErrorCode(int code, String message, String description) {        this.code = code;        this.message = message;        this.description = description;    }    public int getCode() {        return code;    }    public String getMessage() {        return message;    }    public String getDescription() {        return description;    }}

2 定义业务异样类

  • 绝对于 java 的异样类,反对更多字段

    扩大了 codedescription两个字段

  • 自定义构造函数,更灵便 / 快捷的设置字段
import com.leovany.usercenter.common.ErrorCode;/** * 业务异样 * 自定义业务异样类 * * @author leovany * @date 2023/09/23 */public class BusinessException extends RuntimeException {    /**     * 错误码     */    private final int code;    /**     * 形容     */    private final String description;    /**     * 业务异样     *     * @param message     信息     * @param code        错误码     * @param description 形容     */    public BusinessException(String message, int code, String description) {        super(message);        this.code = code;        this.description = description;    }    /**     * 业务异样     *     * @param errorCode 错误代码     */    public BusinessException(ErrorCode errorCode) {        super(errorCode.getMessage());        this.code = errorCode.getCode();        this.description = errorCode.getDescription();    }    /**     * 业务异样     *     * @param errorCode   错误代码     * @param description 形容     */    public BusinessException(ErrorCode errorCode, String description) {        super(errorCode.getMessage());        this.code = errorCode.getCode();        this.description = description;    }    public int getCode() {        return code;    }    public String getDescription() {        return description;    }}

3 全局异样处理器

  • 通过Spring AOP实现,在调用办法前后进行额定的解决
  • 作用

    • 捕捉代码中所有的异样,让前端失去更具体的业务报错信息
    • 屏蔽掉我的项目框架自身的异样,不裸露服务器的外部状态
    • 集中处理,比方还能够做记录日志
import com.leovany.usercenter.common.ResultVO;import com.leovany.usercenter.common.ErrorCode;import com.leovany.usercenter.common.ResultUtils;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestControllerAdvice;/** * 全局异样解决类 */@RestControllerAdvice@Slf4jpublic class GlobalExceptionHandler {    /**     * 解决异样-BusinessException     * @param e     * @return     */    @ExceptionHandler(BusinessException.class)    public ResultVO<?> businessExceptionHandler(BusinessException e){        log.error("businessException:" + e.getMessage(),e);        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());    }    /**     * 解决异样-RuntimeException     * @param e     * @return     */    @ExceptionHandler(RuntimeException.class)    public ResultVO<?> runtimeExceptionHandler(RuntimeException e){        log.error("runtimeException:" + e);        return ResultUtils.error(ErrorCode.ERROR_SYSTEM,e.getMessage());    }}

4 应用

throw new BusinessException能够在办法中,任意中央抛出,很不便

  • 示例代码
@PostMapping("/login")public ResultVO<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {    String userAccount = userLoginRequest.getUserAccount();    String userPassword = userLoginRequest.getUserPassword();    if (StringUtils.isAnyBlank(userAccount, userPassword)) {        throw new BusinessException(ErrorCode.ERROR_PARAMS);    }    User user = userService.doLogin(userAccount, userPassword, request);    return ResultUtils.success(user);}
  • 代码比照

5 前端申请成果

总结

通过封装全局异样解决,对异样信息做了对立解决,让前端失去更具体的业务信息,同时保证系统的安全性(不会裸露零碎外部信息),在代码上对参数校验等方面提供更加不便的模式。