562935844@qq.com
2023-08-31 fdcf461fbfa3bcd650685743e891ad3357898f0c
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
package com.hdl.sdk.connect.cloud;
 
import android.text.TextUtils;
 
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
 
import com.hdl.hdlhttp.callback.HxException;
 
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 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();
    }
 
 
    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(R.string.network_error);
            this.msg = "网络发生异常,请稍后再试";
        }
    }
 
    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;
    }
 
 
    @Override
    public String toString() {
        return "HDLException{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
 
    @Nullable
    @Override
    public String getMessage() {
        return msg;
    }
}