blob: 9647b3f829195c8f0f6db18f6f1e24921a04e6c6 (
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
68
|
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 occurr"),
LABELS_TITLE_MUST_BE_UNIQUE(400, "title must be unique"),
ATTACHMENTS_FILE_ALREADY_EXISTS(409, "File already exists."),
;
private int status;
private String message;
HandledServerErrors(int status, String message) {
this.status = status;
this.message = message;
}
public static HandledServerErrors fromThrowable(Throwable throwable) {
if (throwable instanceof NextcloudHttpRequestFailedException) {
NextcloudHttpRequestFailedException requestFailedException = (NextcloudHttpRequestFailedException) throwable;
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;
}
}
|