blob: 8be6b2b2e718a9b58be823de35b4c2fba279ebed (
plain)
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
|
package it.niedermann.nextcloud.deck.exceptions;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
public enum HandledServerErrors {
UNKNOWN(1337, "hopefully won't occur"),
LABELS_TITLE_MUST_BE_UNIQUE(400, "Title must be unique"),
ATTACHMENTS_FILE_ALREADY_EXISTS(409, "File already exists."),
;
private final int status;
private final String message;
HandledServerErrors(int status, String message) {
this.status = status;
this.message = message;
}
public static HandledServerErrors fromThrowable(Throwable throwable) {
if (throwable instanceof NextcloudHttpRequestFailedException requestFailedException) {
if (requestFailedException.getCause() != null) {
String errorString = requestFailedException.getCause().getMessage();
try {
JsonElement jsonElement = JsonParser.parseString(errorString);
if (jsonElement.isJsonObject()){
ServerError error = new ServerError();
error.status = requestFailedException.getStatusCode();
JsonObject errorObj = jsonElement.getAsJsonObject();
if (errorObj.has("message")){
error.message = errorObj.get("message").getAsString();
}
return findByServerError(error);
}
} catch (JsonSyntaxException e){
return HandledServerErrors.UNKNOWN;
}
}
}
return HandledServerErrors.UNKNOWN;
}
private static HandledServerErrors findByServerError(ServerError error) {
for (HandledServerErrors value : HandledServerErrors.values()) {
if (value.status == error.status && value.message.equals(error.message)){
return value;
}
}
return HandledServerErrors.UNKNOWN;
}
public String getMessage() {
return message;
}
public int getStatus() {
return status;
}
private static class ServerError {
private int status;
private String message;
}
}
|