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
136
137
138
139
140
141
package com.yanzuoguang.util;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.CodeTargetException;
import com.yanzuoguang.util.exception.RuntimeCodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.HashMap;
import java.util.Map;
/**
* 错误处理
*
* @author 颜佐光
*/
public class YzgErrorData {
/**
* 错误标记
*/
public String TAG = StringHelper.EMPTY;
/**
* 错误缓存消息
*/
private final Map<String, String> ErrorCode = new HashMap<>();
public YzgErrorData(String TAG) {
this.TAG = TAG;
}
/**
* 添加错误信息
*
* @param code 错误编码
* @param message 错误消息
*/
public void add(String code, String message) {
ErrorCode.put(code, message);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public CodeException getCodeException(String code, Object... args) {
return getCodeException(null, code, args);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public CodeException getCodeException(Throwable ex, String code, Object... args) {
String message = getMessage(ErrorCode, code, args);
if (ex != null) {
return new CodeException(TAG + code, message, ex);
} else {
return new CodeException(TAG + code, message);
}
}
/**
* 抛出异常
*
* @param code 错误码
* @param target 携带对象
* @param args 异常数据
*/
public CodeTargetException getCodeTargetException(String code, Object target, Object... args) {
return getCodeTargetException(null, code, target, args);
}
/**
* 抛出异常
*
* @param ex 上级错误
* @param code 错误码
* @param target 携带对象
* @param args 异常数据
*/
public CodeTargetException getCodeTargetException(Throwable ex, String code, Object target, Object... args) {
String message = getMessage(ErrorCode, code, args);
if (ex != null) {
return new CodeTargetException(TAG + code, message, target, ex);
} else {
return new CodeTargetException(TAG + code, message, target);
}
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public RuntimeCodeException getRuntimeException(String code, Object... args) {
return getRuntimeException(null, code, args);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public RuntimeCodeException getRuntimeException(Throwable ex, String code, Object... args) {
String message = getMessage(ErrorCode, code, args);
if (ex != null) {
return new RuntimeCodeException(TAG + code, message, ex);
} else {
return new RuntimeCodeException(TAG + code, message);
}
}
/**
* 获取异常消息
*
* @param errorCode
* @param code
* @param args
* @return
*/
public String getMessage(Map<String, String> errorCode, String code, Object... args) {
String message = errorCode.get(code);
if (StringHelper.isEmpty(message)) {
message = "未知错误" + JsonHelper.serialize(args);
} else {
try {
message = String.format(message, args);
} catch (Exception e) {
e.printStackTrace();
}
}
return message;
}
}