forked from slackapi/java-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSCIMApiException.java
More file actions
56 lines (47 loc) · 1.95 KB
/
SCIMApiException.java
File metadata and controls
56 lines (47 loc) · 1.95 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.scim;
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
@EqualsAndHashCode(callSuper = false)
public class SCIMApiException extends Exception {
private final Response response;
private final String responseBody;
private final SCIMApiErrorResponse error;
public SCIMApiException(Response response, String responseBody) {
this(SlackConfig.DEFAULT, response, responseBody);
}
public SCIMApiException(SlackConfig config, Response response, String responseBody) {
this(response, responseBody, parse(config, responseBody));
}
public SCIMApiException(Response response, String responseBody, SCIMApiErrorResponse error) {
super(buildErrorMessage(response, error));
this.response = response;
this.responseBody = responseBody;
this.error = error;
}
private static String buildErrorMessage(Response response, SCIMApiErrorResponse error) {
String message = "status: " + response.code();
if (error != null) {
return message + ", description: " + error.getErrors().getDescription();
} else {
return message + ", no response body";
}
}
private static SCIMApiErrorResponse parse(SlackConfig config, String responseBody) {
SCIMApiErrorResponse parsedErrorResponse = null;
try {
parsedErrorResponse = GsonFactory.createCamelCase(config).fromJson(responseBody, SCIMApiErrorResponse.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;
}
}