-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSkyflowException.java
More file actions
122 lines (101 loc) · 3.56 KB
/
SkyflowException.java
File metadata and controls
122 lines (101 loc) · 3.56 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
package com.skyflow.errors;
import com.google.gson.*;
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;
setRequestId(responseHeaders);
setResponseBody(responseBody);
}
private void setResponseBody(String responseBody) {
try {
if (responseBody != null) {
this.responseBody = JsonParser.parseString(responseBody).getAsJsonObject();
if (this.responseBody.get("error") != null) {
setGrpcCode();
setHttpStatus();
setMessage();
setDetails();
}
}
} catch (JsonSyntaxException e) {
throw new RuntimeException(e);
}
}
public String getRequestId() {
return requestId;
}
private void setRequestId(Map<String, List<String>> responseHeaders) {
if (responseHeaders != null) {
List<String> ids = responseHeaders.get("x-request-id");
this.requestId = ids == null ? null : ids.get(0);
}
}
private void setMessage() {
JsonElement messageElement = ((JsonObject) responseBody.get("error")).get("message");
this.message = messageElement == null ? null : messageElement.getAsString();
}
private void setGrpcCode() {
JsonElement grpcElement = ((JsonObject) responseBody.get("error")).get("grpc_code");
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
}
private void setHttpStatus() {
JsonElement statusElement = ((JsonObject) responseBody.get("error")).get("http_status");
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
}
private void setDetails() {
JsonElement detailsElement = ((JsonObject) responseBody.get("error")).get("details");
this.details = detailsElement == null ? null : detailsElement.getAsJsonArray();
}
public int getHttpCode() {
return httpCode;
}
public JsonArray getDetails() {
return details;
}
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
);
}
}