美文网首页
自定义程序返回异常

自定义程序返回异常

作者: firefly_ | 来源:发表于2019-08-16 23:26 被阅读0次

throw new AppException(ErrorCode.INVALID_PARAMS, "验证码错误");


public class AppException extends RuntimeException {
    private static final long serialVersionUID = 2404372373182554123L;
    private int code;
    private String msg;
    private Object data;

    public AppException() {
        this(ErrorCode.UNKNOW_EXCEPTION);
    }

    public AppException(ErrorInterface code) {
        ErrorInterface code = code == null ? Error.UNKNOW_EXCEPTION : code;
        this.code = ((ErrorInterface)code).getCode();
        this.msg = ((ErrorInterface)code).getMsg();
    }

    public AppException(ErrorInterface code, Exception e) {
        this(code);
        this.addSuppressed(e);
    }

    public AppException(ErrorInterface code, String exMsg) {
        this(code);
        this.msg = String.format(code.getMsg(), exMsg);
    }

    public AppException(ErrorInterface code, String exMsg, Object data) {
        this(code);
        this.msg = String.format(code.getMsg(), exMsg);
        this.data = data;
    }

    public AppException(int code, String exMsg) {
        this.code = code;
        this.msg = exMsg;
    }

    public AppException(int code, String exMsg, Object data) {
        this.code = code;
        this.msg = exMsg;
        this.data = data;
    }

    public String getMessage() {
        return this.msg;
    }

    public int getCode() {
        return this.code;
    }

    public Object getData() {
        return this.data;
    }
}

public interface ErrorInterface {
    String getMsg();

    int getCode();
}

public enum ErrorCode implements ErrorInterface {
    UNKNOW_EXCEPTION(60013, "未知异常~"),
    INIT_FAILED(20004, "Init failed, %s"),
    NOT_ALLOW_MODIFY(20005, "Not Allow Modify, %s"),
    INTERFACE_ERROR(21000, "%s"),
    INVALID_PARAMS(21001, "%s"),
    
    ;

    private int code;
    private String msg;

    ErrorCode(int code) {
        this.code = code;
        this.msg = this.name();
    }

    ErrorCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public String getMsg() {
        return this.msg;
    }

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

相关文章

  • 状态码

    自定义返回码999 自定义的异常错误返回998 无效参数传入997 参数效验失败 801 小程序未配置80...

  • 自定义程序返回异常

    throw new AppException(ErrorCode.INVALID_PARAMS, "验证码错误");

  • 24.自定义规则异常返回

    扩展:自定义异常返回

  • SpringBoot异常处理

    1. 配置全局异常和自定义异常 异常处理类(包括全局和自定义) 自定义异常类 2.返回自定义页面 创建自定义页面位...

  • ssm框架配置全局异常

    要求:既能返回ajax提交请求的异常信息,又能以指定的错误页面返回异常信息***自定义异常 ***指定的异常页面 ...

  • 15/4

    异常: 1.调用abort()函数:程序退出 2.使用返回值,当出现异常时返回false,主动终止程序 3.异常机...

  • python 自定义异常类

    python允许程序员自定义异常,用于描述python中没有涉及的异常情况,自定义异常必须继承Exception类...

  • 2019-01-20

    异常处理,自定义注册异常,多线程基础知识整理 1.异常 1.1异常的概念 异常:在程序编译完成和执行程序过程中,出...

  • Spring 之自定义异常处理基础篇

    自定义异常处理,并且还得需要进行返回到前端为json格式符合RESTful的方式 自定义异常,一般都继承Excep...

  • java优雅处理自定义异常

    自定义异常使用 在java项目中, 一般使用自定义异常对程序做一些特殊处理。使用自定义异常的方式,能更清楚的表现出...

网友评论

      本文标题:自定义程序返回异常

      本文链接:https://www.haomeiwen.com/subject/zbbcsctx.html