接口常见异常响应数据封装

接口常见异常响应数据封装

可以结合短信通知;

邮件通知 参考:https://www.cnblogs.com/subtlman/p/16420546.html

1、异常实体类 exception entity class

package com.example.demo.resp;


/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2020/09/09/9:22
 * @Description: 异常实体类 exception entity class
 */
public final class DomainException implements java.io.Serializable{
    /**
     * 文件名
     * file name
     */
    private String fileName;
    /**
     * 异常类型
     * exception type
     */
    private String exceptionType;
    /**
     * 是否本机方法
     * Is it a native method
     */
    private Boolean nativeMethod;
    /**
     * 13位时间戳
     * 13-bit timestamp
     */
    private Long time;
    /**
     * 错误方法
     * wrong way
     */
    private String methodName;
    /**
     * 错误类
     * error class
     */
    private String className;
    /**
     * 错误行数
     * number of error lines
     */
    private Integer lineNumber;

    public static DomainException getInstance() {
        return new DomainException();
    }

    public DomainException formatException(Exception e){
        this.exceptionType = e+"";
        final StackTraceElement element = e.getStackTrace()[0];
        this.className = element.getClassName();
        this.methodName = element.getMethodName();
        this.fileName = element.getFileName();
        this.lineNumber = element.getLineNumber();
        this.nativeMethod = element.isNativeMethod();
        this.time = java.util.Calendar.getInstance().getTimeInMillis();
        return this;
    }

    @Override
    public String toString() {
        return "{" +
                "fileName='" + fileName + '\'' +
                ", exceptionType='" + exceptionType + '\'' +
                ", nativeMethod=" + nativeMethod +
                ", time='" + time + '\'' +
                ", methodName='" + methodName + '\'' +
                ", className='" + className + '\'' +
                ", lineNumber=" + lineNumber +
                '}';
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getExceptionType() {
        return exceptionType;
    }

    public void setExceptionType(String exceptionType) {
        this.exceptionType = exceptionType;
    }

    public Boolean getNativeMethod() {
        return nativeMethod;
    }

    public void setNativeMethod(Boolean nativeMethod) {
        this.nativeMethod = nativeMethod;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public Integer getLineNumber() {
        return lineNumber;
    }

    public void setLineNumber(Integer lineNumber) {
        this.lineNumber = lineNumber;
    }
}

2、处理程序异常解析器

package com.example.demo.resp;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2020/09/09/9:22
 * @Description: 处理程序异常解析器
 */
@RestControllerAdvice
public final class MyHandlerExceptionResolver{

    /**
     * Remarks:
     * 1、Automatically catch exceptions
     * 2、Manually catch exceptions
     * 3、Fill exception type
     * 4、Does not affect finally use
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public static ServiceResp formatException(Exception e){

        if(e instanceof ArithmeticException){
            return ServiceResp.getInstance().error(
                    "数学运算异常",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof NullPointerException){
            return  ServiceResp.getInstance().error(
                    "程序遇上了空指针",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof ClassNotFoundException){
            return  ServiceResp.getInstance().error(
                    "指定的类不存在",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof ArrayIndexOutOfBoundsException){
            return  ServiceResp.getInstance().error(
                    "数组下标越界",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof IllegalArgumentException){
            return  ServiceResp.getInstance().error(
                    "方法的参数错误",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof IllegalAccessException){
            return  ServiceResp.getInstance().error(
                    "没有访问权限",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof ClassCastException){
            return  ServiceResp.getInstance().error(
                    "类型强制转换异常",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof FileNotFoundException){
            return  ServiceResp.getInstance().error(
                    "文件未找到异常",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof NumberFormatException){
            return  ServiceResp.getInstance().error(
                    "字符串转换为数字异常",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof SQLException){
            return  ServiceResp.getInstance().error(
                    "操作数据库异常",
                    DomainException.getInstance().formatException(e));
        }else if(e instanceof IOException){
            return  ServiceResp.getInstance().error(
                    "输入输出异常",
                    DomainException.getInstance().formatException(e));
        }else {
            return  ServiceResp.getInstance().error(
                    "操作失败",
                    DomainException.getInstance().formatException(e));
        }
    }
}

3、返回对象实体类 return object entity class

package com.example.demo.resp;



/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2020/09/09/9:22
 * @Description: 返回对象实体类 return object entity class
 */
public final class ServiceResp<T> implements java.io.Serializable {

    /**
     * 响应编码:-1失败,0成功
     */
    private Integer code;
    /**
     * 响应信息描述
     */
    private String msg;
    /**
     * 响应内容
     */
    private T data;

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ServiceResp{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public ServiceResp() {
    }

    public static ServiceResp getInstance() {
        return new ServiceResp();
    }

    public ServiceResp error() {
        this.setCode(-1);
        this.setMsg("操作失败");
        this.setData(null);
        return this;
    }

    public ServiceResp error(String msg) {
        this.setCode(-1);
        this.setMsg(msg);
        this.setData(null);
        return this;
    }

    public ServiceResp error(int code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(null);
        return this;
    }

    public ServiceResp error(String msg,T data) {
        this.setCode(-1);
        this.setMsg(msg);
        this.setData(data);
        return this;
    }

    public ServiceResp success() {
        this.setCode(0);
        this.setMsg("操作成功");
        this.setData(null);
        return this;
    }

    public ServiceResp success(String msg) {
        this.setCode(0);
        this.setMsg(msg);
        this.setData(null);
        return this;
    }

    public ServiceResp success(T data, String msg) {
        this.setCode(0);
        this.setMsg(msg);
        this.setData(data);
        return this;
    }

    public ServiceResp success(T data) {
        this.setCode(0);
        this.setMsg("操作成功");
        this.setData(data);
        return this;
    }

    public boolean isSuccess() {
        return this.getCode() == 0 ? true : false;
    }

    public boolean hasRecord() {
        return isSuccess() && this.data != null ? true : false;
    }

}

 

posted @ 2022-08-09 19:15  subtlman  阅读(184)  评论(0)    收藏  举报