-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSkyflowException.java
More file actions
135 lines (114 loc) · 4.68 KB
/
SkyflowException.java
File metadata and controls
135 lines (114 loc) · 4.68 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.skyflow.errors;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.skyflow.utils.Constants;
import java.util.List;
import java.util.Map;
public class SkyflowException extends Exception {
private String requestId;
private Integer grpcCode;
private Integer httpCode;
private String message;
private String httpStatus;
private JsonArray details;
private JsonObject responseBody;
public SkyflowException(String message) {
super(message);
this.message = message;
}
public SkyflowException(Throwable cause) {
super(cause);
this.message = cause.getMessage();
}
public SkyflowException(String message, Throwable cause) {
super(message, cause);
this.message = message;
}
public SkyflowException(int code, String message) {
super(message);
this.httpCode = code;
this.message = message;
this.httpStatus = HttpStatus.BAD_REQUEST.getHttpStatus();
this.details = new JsonArray();
}
public SkyflowException(int httpCode, Throwable cause, Map<String, List<String>> responseHeaders, String responseBody) {
super(cause);
this.httpCode = httpCode > 0 ? httpCode : 400;
try {
setRequestId(responseHeaders);
setResponseBody(responseBody, responseHeaders);
} catch (Exception e) {
this.httpStatus = HttpStatus.BAD_REQUEST.getHttpStatus();
String fullMessage = responseBody != null ? responseBody :
(cause.getLocalizedMessage() != null ? cause.getMessage() : ErrorMessage.ErrorOccurred.getMessage());
this.message = fullMessage.split("HTTP response code:")[0].trim();
}
}
private void setResponseBody(String responseBody, Map<String, List<String>> responseHeaders) {
this.responseBody = JsonParser.parseString(responseBody).getAsJsonObject();
if (this.responseBody.get(Constants.JsonFieldNames.ERROR) != null) {
setGrpcCode();
setHttpStatus();
setMessage();
setDetails(responseHeaders);
}
}
public String getRequestId() {
return requestId;
}
private void setRequestId(Map<String, List<String>> responseHeaders) {
List<String> ids = responseHeaders.get(Constants.REQUEST_ID_HEADER_KEY);
this.requestId = ids == null ? null : ids.get(0);
}
private void setMessage() {
JsonElement messageElement = ((JsonObject) responseBody.get(Constants.JsonFieldNames.ERROR)).get(Constants.JsonFieldNames.MESSAGE);
this.message = messageElement == null ? null : messageElement.getAsString();
}
private void setGrpcCode() {
JsonElement grpcElement = ((JsonObject) responseBody.get(Constants.JsonFieldNames.ERROR)).get(Constants.JsonFieldNames.GRPC_CODE);
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
}
private void setHttpStatus() {
JsonElement statusElement = ((JsonObject) responseBody.get(Constants.JsonFieldNames.ERROR)).get(Constants.JsonFieldNames.HTTP_STATUS);
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
}
public int getHttpCode() {
return httpCode;
}
public JsonArray getDetails() {
return details;
}
private void setDetails(Map<String, List<String>> responseHeaders) {
JsonElement detailsElement = ((JsonObject) responseBody.get(Constants.JsonFieldNames.ERROR)).get(Constants.JsonFieldNames.DETAILS);
List<String> errorFromClientHeader = responseHeaders.get(Constants.ERROR_FROM_CLIENT_HEADER_KEY);
if (detailsElement != null) {
this.details = detailsElement.getAsJsonArray();
}
if (errorFromClientHeader != null) {
this.details = this.details == null ? new JsonArray() : this.details;
String errorFromClient = errorFromClientHeader.get(0);
JsonObject detailObject = new JsonObject();
detailObject.addProperty("errorFromClient", errorFromClient);
this.details.add(detailObject);
}
}
public Integer getGrpcCode() {
return grpcCode;
}
public String getHttpStatus() {
return httpStatus;
}
@Override
public String getMessage() {
return message;
}
@Override
public String toString() {
return String.format(
"%n requestId: %s%n grpcCode: %s%n httpCode: %s%n httpStatus: %s%n message: %s%n details: %s",
this.requestId, this.grpcCode, this.httpCode, this.httpStatus, this.message, this.details
);
}
}