wjc
2023-07-07 22494af577e21a930abef309f2f60c03c9615bd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.hdl.linkpm.sdk.core.exception;
 
import android.text.TextUtils;
 
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
 
import com.hdl.hdlhttp.HxHttpConfig;
import com.hdl.hdlhttp.callback.HxException;
import com.hdl.linkpm.sdk.R;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * Created by Tong on 2021/11/8.
 */
public class HDLException extends RuntimeException {
 
    private Throwable rawThrowable;
    private int code;
    private String msg;
 
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @IntDef({
            ErrorCode.UNKNOWN,
            ErrorCode.PARSE_ERROR,
            ErrorCode.NETWORK_ERROR,
            ErrorCode.HTTP_ERROR,
            ErrorCode.SSL_ERROR,
            ErrorCode.TIMEOUT_ERROR
    })
    public @interface ErrorCode {
        //未知错误
        int UNKNOWN = -40000;
        //解析错误
        int PARSE_ERROR = -40001;
        //网络错误
        int NETWORK_ERROR = -40002;
        //协议出错
        int HTTP_ERROR = -40003;
        //证书出错
        int SSL_ERROR = -40004;
        //连接超时
        int TIMEOUT_ERROR = -40005;
 
    }
 
 
    private int getHxToErrorCode(@HxException.ErrorCode int hxCode) {
        switch (hxCode) {
            case HxException.ErrorCode.UNKNOWN:
                return ErrorCode.UNKNOWN;
            case HxException.ErrorCode.PARSE_ERROR:
                return ErrorCode.PARSE_ERROR;
            case HxException.ErrorCode.NETWORK_ERROR:
                return ErrorCode.NETWORK_ERROR;
            case HxException.ErrorCode.HTTP_ERROR:
                return ErrorCode.HTTP_ERROR;
            case HxException.ErrorCode.SSL_ERROR:
                return ErrorCode.SSL_ERROR;
            case HxException.ErrorCode.TIMEOUT_ERROR:
                return ErrorCode.TIMEOUT_ERROR;
        }
        return ErrorCode.NETWORK_ERROR;
    }
 
    public HDLException(HxException exception) {
        this.code = getHxToErrorCode(exception.getCode());
        this.msg = exception.getMsg();
        this.rawThrowable = exception.getRawThrowable();
    }
 
 
    public HDLException(int code, String msg) {
        this.code = code;
        this.msg = msg;
        if (!TextUtils.isEmpty(msg)) {
            this.msg = msg;
        } else {
            this.msg = HxHttpConfig.getInstance().getString(com.hdl.hdlhttp.R.string.network_error);
        }
    }
 
    public boolean isNetError() {
        return getCode() < 0;
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    @Nullable
    @Override
    public String getMessage() {
        if (rawThrowable != null) {
            String message = rawThrowable.getMessage();
            if (!TextUtils.isEmpty(message)) {
                return message;
            }
            if (!TextUtils.isEmpty(msg)) {
                return msg;
            }
        }
        return super.getMessage();
    }
 
    @Override
    public String toString() {
        return "HDLException{" +
                "rawThrowable=" + rawThrowable +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
 
    public static HDLException getErrorWithCode(HDLErrorCode code) {
        return new HDLException(code.getCode(), code.getMsg());
    }
}