抛出异常时将异常信息返给前端

 

全局异常处理器负责将抛出的异常,以统一的格式返给前端。在这里起主要作用的注解是@RestControllerAdvice。

@RestControllerAdvice主要配合@ExceptionHandler使用,统一处理异常情况。
1、@RestControllerAdvice 注解,可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping中。
2、@RestControllerAdvice 是组件注解,他使得其实现类能够被classpath扫描自动发现,如果应用是通过MVC命令空间或MVC Java编程方式配置,那么该特性默认是自动开启的。

例如:

 当抛出BizException时,会自动返回ReturnResult.error(...)。这样,在真正的业务中,我们只需要返回ReturnResult.success(...)就可以了。

目录结构:

 

具体代码:

import com.sugon.cloud.lowcode.result.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;

import static com.sugon.cloud.lowcode.constants.SplitCharacter.LEFT_PARENTHESES;
import static com.sugon.cloud.lowcode.constants.SplitCharacter.RIGHT_PARENTHESES;
import static com.sugon.cloud.lowcode.result.CodeMessageEnum.*;

/**
 *  全局异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

  /**
   * 业务异常类
   *
   */
  @ExceptionHandler(value = BizException.class)
  @ResponseBody
  public ReturnResult bizExceptionHandler(HttpServletRequest req, BizException e) {
    log.error("发生业务异常: {}, 请求接口: {}", e.getMessage(), req.getRequestURI());
    return ReturnResult.error(e.getCode(), e.getMessage());
  }


  /**
   * 空指针异常
   *
   * @param e 异常信息
   * @param req 请求
   */
  @ExceptionHandler(value = NullPointerException.class)
  @ResponseBody
  public ReturnResult exceptionHandler(HttpServletRequest req, NullPointerException e) {
    log.error("空指针异常信息: {}, 请求接口: {}", e, req.getRequestURI());
    return ReturnResult.error(
        NULL_POINT_ERROR_EXCEPTION.getCode(),
        NULL_POINT_ERROR_EXCEPTION.getMessage()
            + RIGHT_PARENTHESES
            + e.getMessage()
            + LEFT_PARENTHESES);
  }

  /**
   * 接口请求方法异常
   *
   */
  @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
  @ResponseBody
  public ReturnResult methodNotSupportedExceptionHandler(HttpServletRequest req, Exception e) {
    log.error("请求方法异常信息: {},请求接口: {}", e, req.getRequestURI());
    return ReturnResult.error(
        REQUEST_METHOD_ERROR.getCode(),
        REQUEST_METHOD_ERROR.getMessage() + RIGHT_PARENTHESES + e.getMessage() + LEFT_PARENTHESES);
  }

  /**
   * SQL 语法错误异常
   *
   */
  @ExceptionHandler(value = MyBatisSystemException.class)
  @ResponseBody
  public ReturnResult sqlSyntaxErrorExceptionHandler(HttpServletRequest req, Exception e) {
    log.error("MyBatis系统异常信息: {},请求接口: {}", e, req.getRequestURI());
    return ReturnResult.error(
        INNER_FRAME_EXCEPTION.getCode(),
        INNER_FRAME_EXCEPTION.getMessage() + RIGHT_PARENTHESES + e.getMessage() + LEFT_PARENTHESES);
  }

  @ExceptionHandler(value = AuthenticationException.class)
  public ReturnResult incorrectCredentialsException(
      HttpServletRequest request, AuthenticationException e) {
    log.error("用户名或密码不正确: {}, 请求接口: {}", e, request.getRequestURI());
    return ReturnResult.error(USER_CREDENTIALS_ERROR);
  }

  @ExceptionHandler(value = UserException.class)
  public ReturnResult incorrectUserException(HttpServletRequest request, UserException e) {
    log.error("用户信息异常: {}, 请求接口: {}", e, request.getRequestURI());
    return ReturnResult.error(e.getCode(), e.getMessage());
  }

  /**
   * 其他异常信息
   *
   * @param req
   * @param e
   */
  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public ReturnResult exceptionHandler(HttpServletRequest req, Exception e) {
    e.printStackTrace();
    log.error("未知异常: {}, 请求接口: {}", e, req.getRequestURI());
    return ReturnResult.error(
        INTERNAL_SERVER_ERROR.getCode(),
        INTERNAL_SERVER_ERROR.getMessage() + RIGHT_PARENTHESES + e.getMessage() + LEFT_PARENTHESES);
  }
}

自定义的业务异常类:

import com.sugon.cloud.lowcode.result.BaseResultInterface;
import com.sugon.cloud.lowcode.result.CodeMessageEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @desc 业务异常类
 */
@ApiModel(description= "业务异常数据")
public class BizException extends RuntimeException implements BaseResultInterface {

    private static final long serialVersionUID = 1L;


    /**
     * 错误码
     */
    @ApiModelProperty(value = "错误码")
    private String code;

    /**
     * 错误信息
     */
    @ApiModelProperty(value = "错误信息")
    private String message;

    public BizException() {
        super();
    }

    public BizException(CodeMessageEnum codeMessageEnum) {
        super(codeMessageEnum.getCode());
        this.code = codeMessageEnum.getCode();
        this.message = codeMessageEnum.getMessage();
    }

    public BizException(CodeMessageEnum codeMessageEnum, Throwable cause) {
        super(codeMessageEnum.getCode(), cause);
        this.code = codeMessageEnum.getCode();
        this.message = codeMessageEnum.getMessage();
    }

    public BizException(CodeMessageEnum codeMessageEnum, String message, Throwable cause) {
        super(codeMessageEnum.getCode(), cause);
        this.code = codeMessageEnum.getCode();
        this.message = message;
    }

    public BizException(String message) {
        super(message);
        this.message = message;
    }

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

    public BizException(String code, String message, Throwable cause) {
        super(code, cause);
        this.code = code;
        this.message = message;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }


    @Override
    public String getCode() {
        return this.code;
    }

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


对象返回到前端以及异常抛出的接口类:

/**
 * @desc 对象返回到前端以及异常抛出的接口类
 */
public interface BaseResultInterface {

    /**
     * 获取返回代码值
     *
     * @author zby
     * @datetime 2022/11/29 15:23
     */
    String getCode();
    
    /**
     * 获取返回信息
     *
     * @author zby
     * @datetime 2022/11/29 10:26
     */
    String getMessage();
}

异常枚举类:

package com.sugon.cloud.lowcode.result;

import com.sugon.cloud.lowcode.entity.vo.CodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * @desc 定义错误码编码的枚举类
 */
@ApiModel(description = "code码类")
@Api(tags = "code码管理")
public enum CodeMessageEnum implements BaseResultInterface {

  /** ********* 0000成功 *************** */
  SUCCESS("0000", "成功!"),
  SUCCESS_LOGIN("0001", "用户登录成功"),
  SUCCESS_LOGOUT("0002", "用户退出成功"),

  /* 默认失败 */
  COMMON_FAIL("999", "失败"),

  /** ********* 1xxx系统错误 *************** */
  SERVER_BUSY("1001", "服务器正忙,请稍后再试!"),
  INTERNAL_SERVER_ERROR("1002", "服务器内部错误"),
  NOT_FOUND("1003", "未找到该资源!"),
  REQUEST_METHOD_ERROR("1004", "接口请求方法异常"),
  SQL_SYNTAX_ERROR_EXCEPTION("1005", "SQL语法错误异常"),
  NULL_POINT_ERROR_EXCEPTION("1006", "空指针异常信息"),
  INNER_FRAME_EXCEPTION("1007", "内部框架执行异常"),
  PARSE_TOKEN_EXCEPTION("1008", "解析token异常"),
  TOKEN_NOT_EXIST("1009", "token不存在"),
  TOKEN_SIGNATURE_EXCEPTION("1010", "token签名异常"),
  TOKEN_EXPIRE("1011", "token过期,请重新登录"),
  IMG_TYPE_NOT_CONFIG("1012", "请配置图片类型"),
  NOT_CONFIG_FILE_DIR("1013", "请配置文件路径"),
  UPLOAD_FILE_ERROR("1014", "文件上传失败"),
  FILE_NOT_EXIST("1015", "文件不存在"),
  FILE_HAS_DELETED("1016", "文件已被删除"),
  DRIVER_CLASS_NOT_FOUND("1017", "数据库的连接驱动正在加载中..."),
  TRY_CONNECT_DATABASE_ERROR("1018", "尝试连接数据库失败"),
  CLOSE_CONNECT_DATABASE_ERROR("1019", "关闭数据库连接失败"),
  DATABASE_NAME_NOT_EXIST("1020", "数据库名称不存在"),
  CLOSE_DATASOURCE_ERROR("1021", "释放数据库资源异常"),
  DRUID_DATASOURCE_ERROR("1022", "数据源创建中..."),
  CREATE_DATABASE_ERROR("1023", "创建数据库失败"),
  CREATE_TABLE_ERROR("1024", "创建表失败"),
  UPDATE_TABLE_FIELD_ERROR("1025", "更新表字段失败"),
  DELETE_TABLE_FIELD_ERROR("1026", "删除表字段失败"),
  QUERY_ROLE_ERROR("1027", "查询角色失败"),
  UPDATE_GROUP_ERROR("1028", "更新接口组失败"),
  DELETE_GROUP_ERROR("1029", "删除接口组失败"),
  SAVE_INTERFACE_ERROR("1030", "保存接口信息失败"),

  /**
   * ******2xxx参数错误 *********************
   */
  BODY_NOT_MATCH("2001", "请求的数据格式不符"),
  SIGNATURE_NOT_MATCH("2002", "请求的数字签名不匹配!"),
  REQUEST_PATH_NOT_MATCH("2003", "当前请求路径没有权限!"),
  NOT_UPLOAD_FILE_NAME_ERROR("2004", "上传的文件名不存在,请重新上传"),
  NOT_SUPPORT_IMG_TYPE("2005", "图片格式不正确,请重新上传"),
  NOT_SUPPORT_USERNAME_TYPE("2006", "excel用户名不能为空"),
  NOT_SUPPORT_ROLE_TYPE("2007", "角色不存在"),
  NOT_SUPPORT_DEPT_TYPE("2008", "部门不存在"),
  SQL_INJECT_NOT_ALLOWED("2009", "参数中存在数据库关键字,请修改"),
  TABLE_FIELD_NOT_EXIST("2010", "表字段不存在"),

  /** *********** 3xxx用户错误 ******************* */
  USER_NOT_LOGIN("3001", "用户未登录"),
  USER_ACCOUNT_EXPIRED("3002", "账号已过期"),
  USER_CREDENTIALS_ERROR("3003", "用户名或密码错误"),
  USER_CREDENTIALS_EXPIRED("3004", "密码过期"),
  USER_ACCOUNT_NOT_BIND_ENTERPRISE("3005", "当前账号未绑定企业"),
  USER_ACCOUNT_LOCKED("3006", "账号被锁定"),
  USER_ACCOUNT_NOT_EXIST("3007", "账号不存在"),
  USER_ACCOUNT_ALREADY_EXIST("3008", "账号已存在"),
  USER_ACCOUNT_USE_BY_OTHERS("3009", "账号下线"),
  USER_NO_PERMISSION("3010", "当前账号没有此权限"),
  USERNAME_NOT_BLANK("3011", "用户不能为空"),
  USER_LOGIN_ERROR("3012", "用户登录失败"),
  USER_LOGOUT_ERROR("3013", "用户退出失败"),
  USER_ACCOUNT_USE_BY_OTHERS_ERROR("3014", "账号下线异常"),
  USER_ACCESS_DENIED("3015", "权限认证失败"),
  USERNAME_EXIST_ERROR("3016", "用户名重名"),

  ROLE_NAME_ALREADY_EXIST("3101", "角色已存在"),

  /** ********** 4xxx业务错误 *********************** */
  ENTERPRISE_NOT_EXIST("4001", "当前企业不存在"),
  APP_KEY_EXIST("4002", "应用key已存在"),
  APP_NOT_EXIST("4003", "应用不存在"),
  APP_PAGE_NAME_EXIST("4004", "当前页面名称已存在"),
  APP_PAGE_KEY_EXIST("4005", "当前页面key已存在"),
  APP_PAGE_NOT_EXIST("4006", "当前页面不存在,或已删除"),
  APP_PAGE_TYPE_ERROR("4007", "页面类型有误"),
  APP_PAGE_HOME_IS_NOT_EXIST("4008", "请设置首页"),
  CAN_NOT_DELETE_HOME_PAGE("4009", "请勿删除首页"),
  DELETE_PAGE_ERROR("4010", "删除页面失败"),
  CONFIG_CUSTOM_ERROR("4011", "配置自定义页面失败"),
  APP_PAGE_PARENT_NOT_EXIST("4012", "当前页面的父级页面不存在,或已删除"),
  DATASOURCE_NAME_EXIST("4013", "当前数据源名称已经存在,请修改后重试"),
  DATASOURCE_NOT_EXIST("4014", "当前数据源不存在"),
  DATASOURCE_HAS_DELETED("4015", "当前数据源已删除"),
  MODEL_NOT_EXIST("4016", "当前模型不存在"),
  MODEL_HAS_DELETED("4017", "当前模型已删除"),
  MODEL_NAME_HAS_EXIST("4018", "当前模型名称已存在"),
  DATASOURCE_NOT_CONFIG("4019", "数据源配置为空,请联系管理员"),
  DATASOURCE_NOT_CONFIG_DIALECT("4020", "未配置数据源的类型"),
  DATASOURCE_NOT_CONFIG_DRIVER_CLASS_NAME("4021", "未配置数据源的驱动"),
  DEPT_USER_EXIST("4022", "部门下存在用户"),
  NOT_CONFIG_PAGE_BUTTON_TYPE("4023", "未配置按钮雷星"),
  MODEL_PAGE_RELATION_MODEL("4024", "已关联当前模型页面"),
  MODEL_PAGE_NOT_EXIST("4025", "模型页面不存在或已被删除"),
  MODEL_HAS_RELATION_MODEL_PAGE("4026", "当前模型已关联模型页面,不允许删除"),
  FORM_NOT_EXIST("4027", "模型表单不存在"),
  READ_FILE_ERROR("4028", "读取模型页面的模板文件失败"),
  MODEL_PAGE_CONTENT_NULL("4029", "未配置模型页面的模板文件"),
  NOT_CONFIG_QUERY_SQL("4030", "未配置查询语句"),
  APP_PAGE_BUTTON_OPTION_VALUE_ERROR("4031", "未配置接口"),
  DELETE_COLUMN_ERROR("4032", "删除当前失败"),
  INSERT_DATA_ERROR("4033", "新建数据失败"),
  EDIT_DATA_ERROR("4034", "编辑数据失败"),
  DATASOURCE_HAS_MODELS("4035", "当前数据源存在模型,不允许删除"),
  NOT_CONFIG_FORM_API("4036", "未配置模型表单页的接口信息"),
  PLEASE_WRITE_AT_LEAST_DATA("4037", "请至少填写一行数据"),
  AMIS_PAGE_ERROR("4038", "分页参数异常"),
  QUERY_APP_PAGE_QUERY_FIELD_ERROR("4039", "查询搜素参数异常"),
  REQUEST_PARAM_NOT_IN_APP_PAGE_QUERY_FIELD("4040", "请求参数不在查询数据表中"),
  STYLE_LANGUAGE_ON_CSS("4041", "自定义css中的样式语言不能为空"),
  APP_CONFIG_TYPE_NOT_EXIST("4042", "不支持当前应用设置的类型"),
  APP_CONFIG_NOT_EXIST_OR_DELETED("4043", "当前设置不存在或已被删除"),
  APP_HAS_CONFIG_ON_THIS_TYPE("4044", "当前应用设置的类型已存在,请更新"),
  NOT_SUPPORT_COMPONENT_FRAME("4045", "不支持当前组件框架"),
  NOT_SUPPORT_COMPONENT_TYPE("4046", "不支持当前组件类型"),
  CURRENT_APP_KEY_EXIST("4047", "当前应用的组件key已存在"),
  CREATE_CUSTOM_COMPONENT_ERROR("4048", "新增自定义组件失败"),
  APP_CUSTOM_COMPONENT_NOT_EXIST("4049", "当前组件不存在或已被删除"),
  UPDATE_CUSTOM_COMPONENT_ERROR("4050", "更新自定义组件失败"),
  DELETED_CUSTOM_COMPONENT_ERROR("4051", "删除自定义组件失败"),
  INSERT_COMPONENT_DEPENDENCE_ERROR("4052", "新增自定义组件依赖项失败"),
  DELETE_COMPONENT_DEPENDENCE_ERROR("4053", "删除自定义组件依赖项失败"),
  CURRENT_COMPONENT_DEPENDENCE_NOT_EXIST("4054", "当前自定义组件依赖项不存在或已被删除"),
  CURRENT_APP_NAME_EXIST("4055", "当前应用的组件名称已存在"),
  NOT_SUPPORT_DATASOURCE_FROM("4056", "不支持当前的数据库来源"),
  JDBC_CONFIG_ERROR("4057", "平台内置的数据源配置有误"),
  NOT_SUPPORT_MODEL_TYPE("4058", "不支持当前的页面模板"),
  NOT_SUPPORT_CUSTOM_PAGE("4059", "暂不支持自定义页面"),
  FORM_PAGE_ON_DEVELOPING("4060", "自定义页面的该功能正在开发中..."),
  APP_PAGE_QUERY_FIELD_NOT_EXIST("4061", "当前查询条件不存在,或已被删除"),
  APP_PAGE_BUTTON_NOT_EXIST("4062", "当前页面按钮不存在,或已被删除"),
  TABLE_KEY_MORE_THEN_ONE("4063", "主键超过一个"),
  TABLE_KEY_LESS_THEN_ONE("4064", "主键必须存在"),
  TABLE_KEY_MUST_BE_INT("4065", "主键必须为整数类型"),
  TABLE_FIELD_MUST_EXIST("4066", "必须存在表字段"),
  CURRENT_MODEL_PAGE_HAS_CONVERSION_CUSTOM_PAGE("4067", "当前模型页面已转成自定义页面"),
  NOT_SUPPORT_RELEASE_STATUS("4068", "发布版本状态有误"),
  APP_FORM_BUTTON_NOT_EXIST("4067", "当前表单按钮不存在,或已被删除"),
  DATASOURCE_KEY_EXIST_ERROR("4068","数据源key已存在")
  ;

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

  /** 返回到前端的code值 */
  private String code;

  /** 返回到前端的code对应的message值 */
  private String message;

  @Override
  public String getCode() {
    return this.code;
  }

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

  public static CodeVo getJsonObjectByCode(String code) {
    CodeVo codeVo = new CodeVo();
    if (isBlank(code)) {
      return codeVo;
    }
    for (CodeMessageEnum enumObj : CodeMessageEnum.values()) {
      if (enumObj.getCode().equals(code)) {
        return new CodeVo(code, enumObj.message);
      }
    }
    return codeVo;
  }
}

统一给前端的结果格式:

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import static com.sugon.cloud.lowcode.result.CodeMessageEnum.*;

/**
 * @author zby
 * @datetime 2022/12/2 10:23
 * @desc 返回给前端的对象值
 */
@Setter
@Getter
@ApiModel(description= "返回响应数据")
public class ReturnResult<T> {

  /** 响应代码 */
  @ApiModelProperty(value = "状态码")
  private String code;

  /** 响应消息 */
  @ApiModelProperty(value = "响应信息")
  private String message;

  /** 响应结果 */
  @ApiModelProperty(value = "响应对象")
  private T result;

  /** 返回成功还是失败 */
  @ApiModelProperty(value = "是否成功")
  private boolean success = true;

  public ReturnResult() {}

  public ReturnResult(CodeMessageEnum codeMessageEnum) {
    this.code = codeMessageEnum.getCode();
    this.message = codeMessageEnum.getMessage();
  }

  /**
   * 返回成功
   *
   * @author zby
   * @datetime 2022/12/2 10:29
   */
  public static ReturnResult success() {
    return success(null);
  }

  /**
   * 返回成功
   *
   * @param data 返回到前端的对象
   * @author zby
   * @datetime 2022/12/2 13:33
   */
  public static <T> ReturnResult<T> success(T data) {
    return success(SUCCESS, data);
  }

  /**
   * 登录成功
   *
   * @param data 返回到前端的对象
   * @author zby
   * @datetime 2022/12/2 13:50
   */
  public static <T> ReturnResult<T> loginSuccess(T data) {
    return success(SUCCESS_LOGIN, data);
  }

  /**
   * 退出成功
   *
   * @author zby
   * @datetime 2022/11/30 14:53
   */
  public static ReturnResult loginOutSuccess() {
    return success(SUCCESS_LOGOUT, null);
  }

  private static <T> ReturnResult<T> success(CodeMessageEnum codeMessageEnum, T data) {
    ReturnResult<T> result = new ReturnResult<>();
    result.setCode(codeMessageEnum.getCode());
    result.setMessage(codeMessageEnum.getMessage());
    result.setResult(data);
    return result;
  }

  /**
   * 返回失败
   *
   * @param codeMessageEnum 基础返回接口
   * @author zby
   * @datetime 2022/12/2 14:50
   */
  public static ReturnResult error(CodeMessageEnum codeMessageEnum) {
    return error(codeMessageEnum.getCode(), codeMessageEnum.getMessage());
  }

  /**
   * 返回失败
   *
   * @param codeMessageEnum
   * @param message
   * @author zby
   * @datetime 2022/11/30 14:50
   */
  public static ReturnResult error(CodeMessageEnum codeMessageEnum, String message) {
    return error(codeMessageEnum.getCode(), message);
  }

  /**
   * 返回失败
   *
   * @param code
   * @param message
   * @author zby
   * @datetime 2022/11/28 16:23
   */
  public static ReturnResult error(String code, String message) {
    ReturnResult result = new ReturnResult();
    result.setCode(code);
    result.setMessage(message);
    result.setSuccess(false);
    return result;
  }

  /**
   * 重写toString方法
   *
   * @author zby
   * @datetime 2022/11/29 13:44
   */
  @Override
  public String toString() {
    return JSONObject.toJSONString(this);
  }
}