-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathCosServiceExceptionBuilder.java
216 lines (180 loc) · 6.15 KB
/
CosServiceExceptionBuilder.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
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
* According to cos feature, we modify some class,comment, field name, etc.
*/
package com.qcloud.cos.exception;
import java.util.HashMap;
import java.util.Map;
import com.qcloud.cos.exception.CosServiceException.ErrorType;
public class CosServiceExceptionBuilder {
/**
* The unique COS identifier for the service request the caller made. The COS request ID can
* uniquely identify the COS request.
*/
private String requestId;
/**
* The COS error code represented by this exception (ex: InvalidParameterValue).
*/
private String errorCode;
/**
* The error message as returned by the service.
*/
private String errorMessage;
/**
* The HTTP status code that was returned with this error
*/
private int statusCode;
/**
* An COS specific request ID that provides additional debugging information.
*/
private String traceId;
/**
* Additional information on the exception.
*/
private Map<String, String> additionalDetails;
/**
* header information
*/
private Map<String, String> headers = new HashMap<String, String>();
/**
* Returns the error XML received in the HTTP Response or null if the exception is constructed
* from the headers.
*/
private String errorResponseXml;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorCode() {
return errorCode;
}
/**
* Returns the human-readable error message provided by the service
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* Sets the human-readable error message provided by the service
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
/**
* Sets the HTTP status code that was returned with this service exception.
*
* @param statusCode The HTTP status code that was returned with this service exception.
*/
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
/**
* Returns the HTTP status code that was returned with this service exception.
*
* @return The HTTP status code that was returned with this service exception.
*/
public int getStatusCode() {
return statusCode;
}
/**
* Gets Qcloud COS's extended request ID. This ID is required debugging information in the case
*
* @return Qcloud COS's extended request ID.
*/
public String getTraceId() {
return traceId;
}
public void setTraceId(String traceId) {
this.traceId = traceId;
}
/**
* Returns any additional information retrieved in the error response.
*/
public Map<String, String> getAdditionalDetails() {
return additionalDetails;
}
/**
* Sets additional information about the response.
*/
public void setAdditionalDetails(Map<String, String> additionalDetails) {
this.additionalDetails = additionalDetails;
}
/**
* Adds an entry to the additional information map.
*/
public void addAdditionalDetail(String key, String detail) {
if (detail == null || detail.trim().isEmpty()) {
return;
}
if (this.additionalDetails == null) {
this.additionalDetails = new HashMap<String, String>();
}
String additionalContent = this.additionalDetails.get(key);
if (additionalContent != null && !additionalContent.trim().isEmpty()) {
detail = additionalContent + "-" + detail;
}
if (!detail.isEmpty()) {
additionalDetails.put(key, detail);
}
}
/**
* Returns the original error response XML received from Qcloud COS
*/
public String getErrorResponseXml() {
return errorResponseXml;
}
/**
* Sets the error response XML received from Cos
*/
public void setErrorResponseXml(String errorResponseXml) {
this.errorResponseXml = errorResponseXml;
}
/**
* Creates a new CosServiceException object with the values set.
*/
public CosServiceException build() {
CosServiceException cosException =
errorResponseXml == null ? new CosServiceException(errorMessage)
: new CosServiceException(errorMessage, errorResponseXml);
cosException.setErrorCode(errorCode);
cosException.setTraceId(traceId);
cosException.setStatusCode(statusCode);
cosException.setRequestId(requestId);
cosException.setAdditionalDetails(additionalDetails);
cosException.setErrorType(errorTypeOf(statusCode));
cosException.setHeaders(headers);
return cosException;
}
/**
* Returns the COS error type information by looking at the HTTP status code in the error
* response. COS error responses don't explicitly declare a sender or client fault like other COS
* services, so we have to use the HTTP status code to infer this information.
*
* @param httpResponse The HTTP error response to use to determine the right error type to set.
*/
private ErrorType errorTypeOf(int statusCode) {
return statusCode >= 500 ? ErrorType.Service : ErrorType.Client;
}
public Map<String, String> getHeaders() {
return headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
}