-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathServiceException.java
311 lines (281 loc) · 9.16 KB
/
ServiceException.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.aliyun.oss;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* <p>
* This is the base exception class to represent any expected or unexpected OSS
* server side errors.
* </p>
*
* <p>
* {@link ServiceException} is converted from error code from OSS response. For
* example, when OSS tries to authenticate a request, if Access ID does not
* exist, the SDK will throw a {@link ServiceException} or its subclass instance
* with the specific error code, which the caller could handle that with
* specific logic.
* </p>
*
* <p>
* On the other side, {@link ClientException} is the class to represent any
* exception in OSS client side. Generally ClientException occurs either before
* sending the request or after receving the response from OSS server side. For
* example, if the network is broken when it tries to send a request, then the
* SDK will throw a {@link ClientException} instance.
* </p>
*
* <p>
* So generally speaking, the caller only needs to handle
* {@link ServiceException} properly as it means the request is processed, but
* not completely finished due to different errors. The error code in the
* exception is a good diagnostics information. Sometimes these exceptions are
* completely expected.
* </p>
*/
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = 430933593095358673L;
private String errorMessage;
private String errorCode;
private String requestId;
private String hostId;
private String ec;
private String rawResponseError;
private Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
private Map<String, Object> errorFields = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
/**
* Creates a default instance.
*/
public ServiceException() {
super();
}
/**
* Creates an instance with the error message.
*
* @param errorMessage
* Error message.
*/
public ServiceException(String errorMessage) {
super((String) null);
this.errorMessage = errorMessage;
}
/**
* Creates an instance with a {@link Throwable} instance.
*
* @param cause
* A {@link Throwable} instance.
*/
public ServiceException(Throwable cause) {
super(null, cause);
}
/**
* Creates an instance with a {@link Throwable} instance and error message.
*
* @param errorMessage
* Error message.
* @param cause
* A {@link Throwable} instance.
*/
public ServiceException(String errorMessage, Throwable cause) {
super(null, cause);
this.errorMessage = errorMessage;
}
/**
* Creates an instance with error message, error code, request id, host id.
*
* @param errorMessage
* Error message.
* @param errorCode
* Error code.
* @param requestId
* Request Id.
* @param hostId
* Host Id.
*/
public ServiceException(String errorMessage, String errorCode, String requestId, String hostId) {
this(errorMessage, errorCode, requestId, hostId, null);
}
/**
* Creates an instance with error message, error code, request id, host id.
*
* @param errorMessage
* Error message.
* @param errorCode
* Error code.
* @param requestId
* Request Id.
* @param hostId
* Host Id.
* @param cause
* A {@link Throwable} instance indicates a specific exception.
*/
public ServiceException(String errorMessage, String errorCode, String requestId, String hostId, Throwable cause) {
this(errorMessage, errorCode, requestId, hostId, null, cause);
}
/**
* Creates an instance with error message, error code, request id, host id,
* OSS response error, and a Throwable instance.
*
* @param errorMessage
* Error message.
* @param errorCode
* Error code.
* @param requestId
* Request Id.
* @param hostId
* Host Id.
* @param rawResponseError
* OSS error message in response.
* @param cause
* A {@link Throwable} instance indicates a specific exception.
*/
public ServiceException(String errorMessage, String errorCode, String requestId, String hostId,
String rawResponseError, Throwable cause) {
this(errorMessage, errorCode, requestId, hostId, rawResponseError, cause, null);
}
/**
* Creates an instance with error message, error code, request id, host id,
* OSS response error, and a Throwable instance.
*
* @param errorMessage
* Error message.
* @param errorCode
* Error code.
* @param requestId
* Request Id.
* @param hostId
* Host Id.
* @param rawResponseError
* OSS error message in response.
* @param cause
* A {@link Throwable} instance indicates a specific exception.
* @param ec
* OSS error code in XXXX-XXXXXXXX format.
*/
public ServiceException(String errorMessage, String errorCode, String requestId, String hostId,
String rawResponseError, Throwable cause, String ec) {
this(errorMessage, cause);
this.errorCode = errorCode;
this.requestId = requestId;
this.hostId = hostId;
this.rawResponseError = rawResponseError;
this.ec = ec;
}
public ServiceException(String errorMessage, String errorCode, String requestId, String hostId,
String rawResponseError, Throwable cause, String ec, Map<String, String> headers, Map<String, Object> errorFields) {
this(errorMessage, cause);
this.errorCode = errorCode;
this.requestId = requestId;
this.hostId = hostId;
this.rawResponseError = rawResponseError;
this.ec = ec;
this.headers = headers;
this.errorFields = errorFields;
}
/**
* Gets error message.
*
* @return Error message.
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* Gets the error code.
*
* @return The error code in string.
*/
public String getErrorCode() {
return errorCode;
}
/**
* Gets the request id.
*
* @return The request Id in string.
*/
public String getRequestId() {
return requestId;
}
/**
* Gets the host id.
*
* @return The host Id in string.
*/
public String getHostId() {
return hostId;
}
/**
* Gets the error message in OSS response.
*
* @return Error response in string.
*/
public String getRawResponseError() {
return rawResponseError;
}
/**
* Sets the error response from OSS.
*
* @param rawResponseError
* The error response from OSS.
*/
public void setRawResponseError(String rawResponseError) {
this.rawResponseError = rawResponseError;
}
/**
* Gets the ec.
*
* @return The ec in string.
*/
public String getEC() {
return ec;
}
private String formatRawResponseError() {
if (rawResponseError == null || rawResponseError.equals("")) {
return "";
}
return String.format("\n[ResponseError]:\n%s", this.rawResponseError);
}
public Map<String, String> getHeaders() {
return headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public Map<String, Object> getErrorFields() {
return errorFields;
}
public void setErrorFields(Map<String, Object> errorFields) {
this.errorFields = errorFields;
}
@Override
public String getMessage() {
String msg = getErrorMessage() + "\n[ErrorCode]: " + getErrorCode() + "\n[RequestId]: " + getRequestId()
+ "\n[HostId]: " + getHostId();
if (ec != null) {
msg += "\n[EC]: " + getEC();
}
return msg + formatRawResponseError();
}
}