其他分享
首页 > 其他分享> > 9.一套简单的全局错误处理框架,工作中直接拿来可用

9.一套简单的全局错误处理框架,工作中直接拿来可用

作者:互联网

1.先定位错误接口

public interface BaseErrorInfoInterface {

  String getResultCode();

  String getResultMsg();
}

2.定义错误枚举

public enum  CommonEnum implements BaseErrorInfoInterface {
  // 数据操作错误定义
  SUCCESS("200", "成功!"),
  BODY_NOT_MATCH("400","请求的数据格式不符!"),
  NULL_POINT("407","出现了空指针异常!"),
  SIGNATURE_NOT_MATCH("401","请求的数字签名不匹配!"),
  NOT_FOUND("404", "未找到该资源!"),
  INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
  SERVER_BUSY("503","服务器正忙,请稍后再试!");

  /** 错误码 */
  private String resultCode;

  /** 错误描述 */
  private String resultMsg;

  CommonEnum(String resultCode, String resultMsg) {
    this.resultCode = resultCode;
    this.resultMsg = resultMsg;
  }

  @Override
  public String getResultCode() {
    return resultCode;
  }

  @Override
  public String getResultMsg() {
    return resultMsg;
  }
}

3.定义我们的返回值

public class ResultBody {
  /**
   * 响应代码
   */
  private String code;

  /**
   * 响应消息
   */
  private String message;

  /**
   * 响应结果
   */
  private Object result;

  public ResultBody() {
  }

  public ResultBody(BaseErrorInfoInterface errorInfo) {
    this.code = errorInfo.getResultCode();
    this.message = errorInfo.getResultMsg();
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public Object getResult() {
    return result;
  }

  public void setResult(Object result) {
    this.result = result;
  }

  /**
   * 成功
   *
   * @return
   */
  public static ResultBody success() {
    return success(null);
  }

  /**
   * 成功
   * @param data
   * @return
   */
  public static ResultBody success(Object data) {
    ResultBody rb = new ResultBody();
    rb.setCode(CommonEnum.SUCCESS.getResultCode());
    rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
    rb.setResult(data);
    return rb;
  }

  /**
   * 失败
   */
  public static ResultBody error(BaseErrorInfoInterface errorInfo) {
    ResultBody rb = new ResultBody();
    rb.setCode(errorInfo.getResultCode());
    rb.setMessage(errorInfo.getResultMsg());
    rb.setResult(null);
    return rb;
  }

  /**
   * 失败
   */
  public static ResultBody error(String code, String message) {
    ResultBody rb = new ResultBody();
    rb.setCode(code);
    rb.setMessage(message);
    rb.setResult(null);
    return rb;
  }

  /**
   * 失败
   */
  public static ResultBody error( String message) {
    ResultBody rb = new ResultBody();
    rb.setCode("-1");
    rb.setMessage(message);
    rb.setResult(null);
    return rb;
  }

  @Override
  public String toString() {
    return JSONObject.toJSONString(this);
  }
}

4.定义业务异常

public class BizException extends RuntimeException{
  private static final long serialVersionUID = 1L;

  /**
   * 错误码
   */
  protected String errorCode;
  /**
   * 错误信息
   */
  protected String errorMsg;

  public BizException() {
    super();
  }

  public BizException(BaseErrorInfoInterface errorInfoInterface) {
    super(errorInfoInterface.getResultCode());
    this.errorCode = errorInfoInterface.getResultCode();
    this.errorMsg = errorInfoInterface.getResultMsg();
  }

  public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
    super(errorInfoInterface.getResultCode(), cause);
    this.errorCode = errorInfoInterface.getResultCode();
    this.errorMsg = errorInfoInterface.getResultMsg();
  }

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

  public BizException(String errorCode, String errorMsg) {
    super(errorCode);
    this.errorCode = errorCode;
    this.errorMsg = errorMsg;
  }

  public BizException(String errorCode, String errorMsg, Throwable cause) {
    super(errorCode, cause);
    this.errorCode = errorCode;
    this.errorMsg = errorMsg;
  }


  public String getErrorCode() {
    return errorCode;
  }

  public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
  }

  public String getErrorMsg() {
    return errorMsg;
  }

  public void setErrorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
  }

  public String getMessage() {
    return errorMsg;
  }

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

}

5.定义springboot全局错误处理

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {

  /**
   * 处理自定义的业务异常
   * @param req
   * @param e
   * @return
   */
  @ExceptionHandler(value = BizException.class)
  @ResponseBody
  public  ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){
    log.error("发生业务异常!原因是:{}",e.getErrorMsg());
    return ResultBody.error(e.getErrorCode(),e.getErrorMsg());
  }

  /**
   * 处理空指针的异常
   * @param req
   * @param e
   * @return
   */
  @ExceptionHandler(value =NullPointerException.class)
  @ResponseBody
  public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
    log.error("发生空指针异常!原因是:",e);
    return ResultBody.error(CommonEnum.NULL_POINT);
  }


  /**
   * 处理其他异常
   * @param req
   * @param e
   * @return
   */
  @ExceptionHandler(value =Exception.class)
  @ResponseBody
  public ResultBody exceptionHandler(HttpServletRequest req, Exception e){
    log.error("未知异常!原因是:",e);
    return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
  }
}

6.常用的业务请求,基本涵盖各种场景

@RestController
public class BusinessController {

  @GetMapping("/success")
  public ResultBody success(){
    return ResultBody.success("zhangsan");
  }

  @GetMapping("/nullPoint")
  public ResultBody nullPoint(){
    List<String> list = null;
    list.add("lisi");
    return ResultBody.success();
  }

  @GetMapping("/others")
  public ResultBody others() throws FileNotFoundException {

    throw new FileNotFoundException();
  }

  @GetMapping("/bizException")
  public ResultBody bizException(){
    throw new BizException(CommonEnum.BODY_NOT_MATCH.getResultCode(),"出现了我们业务的错误");
  }
}

7.返回情况

 

 

 

 

 

 

 

标签:return,String,ResultBody,全局,public,rb,errorMsg,错误处理,拿来
来源: https://www.cnblogs.com/johnzhao/p/15014660.html