/
GoogleJsonError.java
344 lines (292 loc) · 8.75 KB
/
GoogleJsonError.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2011 Google Inc.
*
* Licensed 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.google.api.client.googleapis.json;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.util.Data;
import com.google.api.client.util.Key;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* Data class representing the Google JSON error response content, as documented for example in <a
* href="https://developers.google.com/url-shortener/v1/getting_started#errors">Error responses</a>.
*
* @since 1.4
* @author Yaniv Inbar
*/
public class GoogleJsonError extends GenericJson {
/**
* Parses the given error HTTP response using the given JSON factory.
*
* @param jsonFactory JSON factory
* @param response HTTP response
* @return new instance of the Google JSON error information
* @throws IllegalArgumentException if content type is not {@link Json#MEDIA_TYPE} or if expected
* {@code "data"} or {@code "error"} key is not found
*/
public static GoogleJsonError parse(JsonFactory jsonFactory, HttpResponse response)
throws IOException {
JsonObjectParser jsonObjectParser =
new JsonObjectParser.Builder(jsonFactory)
.setWrapperKeys(Collections.singleton("error"))
.build();
return jsonObjectParser.parseAndClose(
response.getContent(), response.getContentCharset(), GoogleJsonError.class);
}
static {
// hack to force ProGuard to consider ErrorInfo used, since otherwise it would be stripped out
// see https://github.com/googleapis/google-api-java-client/issues/527
Data.nullOf(ErrorInfo.class);
}
/** Detailed error information. */
public static class ErrorInfo extends GenericJson {
/** Error classification or {@code null} for none. */
@Key private String domain;
/** Error reason or {@code null} for none. */
@Key private String reason;
/** Human readable explanation of the error or {@code null} for none. */
@Key private String message;
/**
* Location in the request that caused the error or {@code null} for none or {@code null} for
* none.
*/
@Key private String location;
/** Type of location in the request that caused the error or {@code null} for none. */
@Key private String locationType;
/**
* Returns the error classification or {@code null} for none.
*
* @since 1.8
*/
public final String getDomain() {
return domain;
}
/**
* Sets the error classification or {@code null} for none.
*
* @since 1.8
*/
public final void setDomain(String domain) {
this.domain = domain;
}
/**
* Returns the error reason or {@code null} for none.
*
* @since 1.8
*/
public final String getReason() {
return reason;
}
/**
* Sets the error reason or {@code null} for none.
*
* @since 1.8
*/
public final void setReason(String reason) {
this.reason = reason;
}
/**
* Returns the human readable explanation of the error or {@code null} for none.
*
* @since 1.8
*/
public final String getMessage() {
return message;
}
/**
* Sets the human readable explanation of the error or {@code null} for none.
*
* @since 1.8
*/
public final void setMessage(String message) {
this.message = message;
}
/**
* Returns the location in the request that caused the error or {@code null} for none or {@code
* null} for none.
*
* @since 1.8
*/
public final String getLocation() {
return location;
}
/**
* Sets the location in the request that caused the error or {@code null} for none or {@code
* null} for none.
*
* @since 1.8
*/
public final void setLocation(String location) {
this.location = location;
}
/**
* Returns the type of location in the request that caused the error or {@code null} for none.
*
* @since 1.8
*/
public final String getLocationType() {
return locationType;
}
/**
* Sets the type of location in the request that caused the error or {@code null} for none.
*
* @since 1.8
*/
public final void setLocationType(String locationType) {
this.locationType = locationType;
}
@Override
public ErrorInfo set(String fieldName, Object value) {
return (ErrorInfo) super.set(fieldName, value);
}
@Override
public ErrorInfo clone() {
return (ErrorInfo) super.clone();
}
}
public static class Details {
@Key("@type")
private String type;
@Key private String detail;
@Key private String reason;
@Key private List<ParameterViolations> parameterViolations;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public List<ParameterViolations> getParameterViolations() {
return parameterViolations;
}
/**
* Sets parameterViolations list as immutable to prevent exposing mutable state.
*
* @param parameterViolations
*/
public void setParameterViolations(List<ParameterViolations> parameterViolations) {
this.parameterViolations = ImmutableList.copyOf(parameterViolations);
}
}
public static class ParameterViolations {
@Key private String parameter;
@Key private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
/** List of detailed errors or {@code null} for none. */
@Key private List<ErrorInfo> errors;
/** HTTP status code of this response or {@code null} for none. */
@Key private int code;
/** Human-readable explanation of the error or {@code null} for none. */
@Key private String message;
/** Lists type and parameterViolation details of an Exception */
@Key private List<Details> details;
/**
* Returns the list of detailed errors or {@code null} for none.
*
* @since 1.8
*/
public final List<ErrorInfo> getErrors() {
return errors;
}
/**
* Sets the list of detailed errors or {@code null} for none. Sets the list of detailed errors as
* immutable to prevent exposing mutable state.
*
* @since 1.8
*/
public final void setErrors(List<ErrorInfo> errors) {
this.errors = ImmutableList.copyOf(errors);
}
/**
* Returns the HTTP status code of this response or {@code null} for none.
*
* @since 1.8
*/
public final int getCode() {
return code;
}
/**
* Sets the HTTP status code of this response or {@code null} for none.
*
* @since 1.8
*/
public final void setCode(int code) {
this.code = code;
}
/**
* Returns the human-readable explanation of the error or {@code null} for none.
*
* @since 1.8
*/
public final String getMessage() {
return message;
}
/**
* Sets the human-readable explanation of the error or {@code null} for none.
*
* @since 1.8
*/
public final void setMessage(String message) {
this.message = message;
}
public List<Details> getDetails() {
return details;
}
/**
* Sets the list of invalid parameter error details as immutable to prevent exposing mutable
* state.
*
* @param details
*/
public void setDetails(List<Details> details) {
this.details = ImmutableList.copyOf(details);
}
@Override
public GoogleJsonError set(String fieldName, Object value) {
return (GoogleJsonError) super.set(fieldName, value);
}
@Override
public GoogleJsonError clone() {
return (GoogleJsonError) super.clone();
}
}