Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HandledServerErrors.java « exceptions « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5d3e31553c7e735747882e5d0138faa02dc4ded (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 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) {
            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;
    }
}