-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathErrorDeserializer.java
More file actions
119 lines (105 loc) · 4.61 KB
/
ErrorDeserializer.java
File metadata and controls
119 lines (105 loc) · 4.61 KB
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
package com.easypost.model;
import com.easypost.Constants;
import com.easypost.exception.APIException;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public final class ErrorDeserializer implements JsonDeserializer<APIException> {
/**
* Recursively traverse an error JSON element and its sub-element(s), and
* extracts all
* error string values found into the specified string list.
*
* @param element the JSON element to traverse
* @param messagesList the list of strings to append found values to
*/
private void traverseJsonElement(JsonElement element, ArrayList<String> messagesList) {
if (element.isJsonPrimitive()) {
messagesList.add(element.getAsString());
} else if (element.isJsonArray()) {
JsonArray array = element.getAsJsonArray();
for (JsonElement arrayElement : array) {
traverseJsonElement(arrayElement, messagesList);
}
} else if (element.isJsonObject()) {
JsonObject object = element.getAsJsonObject();
for (Entry<String, JsonElement> entry : object.entrySet()) {
traverseJsonElement(entry.getValue(), messagesList);
}
}
}
/**
* Deserialize an APIException from a JSON object.
*
* @param json JSON object to deserialize.
* @param typeOfT Type of the object to deserialize.
* @param context Deserialization context.
* @return Deserialized APIException object.
* @throws JsonParseException if the JSON object is not a valid
* SmartRateCollection.
*/
@Override
public APIException deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
JsonObject jo = json.getAsJsonObject();
String message = null;
String code = null;
List<Object> errors = new ArrayList<>();
JsonElement errorResponse = jo.get("error");
if (errorResponse == null) {
message = Constants.ErrorMessages.API_DID_NOT_RETURN_ERROR_DETAILS;
code = "NO RESPONSE CODE";
return new APIException(message, code, null);
}
JsonObject errorData = errorResponse.getAsJsonObject();
JsonElement codeElement = errorData.get("code");
if (codeElement != null) {
code = codeElement.getAsString();
}
JsonElement messageElement = errorData.get("message");
if (messageElement != null) {
if (messageElement.isJsonPrimitive()) {
message = messageElement.getAsString();
} else if (messageElement.isJsonObject() || messageElement.isJsonArray()) {
ArrayList<String> messagesList = new ArrayList<>();
traverseJsonElement(messageElement, messagesList);
message = String.join(", ", messagesList);
} else {
throw new JsonParseException("Invalid message format");
}
}
JsonElement errorsAsJson = errorData.get("errors");
if (errorsAsJson != null) {
JsonArray errorsAsArray = errorsAsJson.getAsJsonArray();
for (JsonElement errorAsJson : errorsAsArray) {
if (errorAsJson.isJsonObject()) {
JsonObject errorAsJsonObject = errorAsJson.getAsJsonObject();
FieldError fieldError = new FieldError();
JsonElement field = errorAsJsonObject.get("field");
if (field != null) {
fieldError.setField(field.getAsString());
}
JsonElement fieldMessage = errorAsJsonObject.get("message");
if (fieldMessage != null) {
fieldError.setMessage(fieldMessage.getAsString());
}
JsonElement suggestion = errorAsJsonObject.get("suggestion");
if (suggestion != null && !suggestion.isJsonNull()) {
fieldError.setSuggestion(suggestion.getAsString());
}
errors.add(fieldError);
} else {
errors.add(errorAsJson.getAsString());
}
}
}
return new APIException(message, code, errors);
}
}