forked from slackapi/java-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlackApiException.java
More file actions
56 lines (46 loc) · 2.01 KB
/
SlackApiException.java
File metadata and controls
56 lines (46 loc) · 2.01 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
package com.slack.api.methods;
import com.slack.api.SlackConfig;
import com.slack.api.util.json.GsonFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
@Data
@Slf4j
public class SlackApiException extends Exception {
private final Response response;
private final String responseBody;
private final SlackApiErrorResponse error;
public SlackApiException(Response response, String responseBody) {
this(SlackConfig.DEFAULT, response, responseBody);
}
public SlackApiException(SlackConfig config, Response response, String responseBody) {
this(response, responseBody, parse(config, responseBody));
}
public SlackApiException(Response response, String responseBody, SlackApiErrorResponse error) {
super(buildErrorMessage(response, error));
this.response = response;
this.responseBody = responseBody;
this.error = error;
}
private static String buildErrorMessage(Response response, SlackApiErrorResponse error) {
String message = "status: " + response.code();
if (error != null) {
return message + ", error: " + error.getError() + ", needed: " + error.getNeeded() + ", provided: " + error.getProvided() + ", warning: " + error.getWarning();
} else {
return message + ", no response body";
}
}
private static SlackApiErrorResponse parse(SlackConfig config, String responseBody) {
SlackApiErrorResponse parsedErrorResponse = null;
try {
parsedErrorResponse = GsonFactory.createSnakeCase(config).fromJson(responseBody, SlackApiErrorResponse.class);
} catch (Exception e) {
if (log.isDebugEnabled()) {
String responseToPrint = responseBody.length() > 1000 ? responseBody.subSequence(0, 1000) + " ..." : responseBody;
log.debug("Failed to parse the error response body: {}", responseToPrint);
}
}
return parsedErrorResponse;
}
}