summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/push/autopush/AutopushClientException.java
blob: e3fda7a45fe66f8747b3505a60d4096362a6a71e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.push.autopush;

import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.HttpStatus;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.HTTPFailureException;
import org.mozilla.gecko.sync.net.SyncStorageResponse;

public class AutopushClientException extends Exception {
    private static final long serialVersionUID = 7953459541558266500L;

    public AutopushClientException(String detailMessage) {
        super(detailMessage);
    }

    public AutopushClientException(Exception e) {
        super(e);
    }

    public boolean isTransientError() {
        return false;
    }

    public static class AutopushClientRemoteException extends AutopushClientException {
        private static final long serialVersionUID = 2209313149952001000L;

        public final HttpResponse response;
        public final long httpStatusCode;
        public final long apiErrorNumber;
        public final String error;
        public final String message;
        public final ExtendedJSONObject body;

        public AutopushClientRemoteException(HttpResponse response, long httpStatusCode, long apiErrorNumber, String error, String message, ExtendedJSONObject body) {
            super(new HTTPFailureException(new SyncStorageResponse(response)));
            if (body == null) {
                throw new IllegalArgumentException("body must not be null");
            }
            this.response = response;
            this.httpStatusCode = httpStatusCode;
            this.apiErrorNumber = apiErrorNumber;
            this.error = error;
            this.message = message;
            this.body = body;
        }

        @Override
        public String toString() {
            return "<AutopushClientRemoteException " + this.httpStatusCode + " [" + this.apiErrorNumber + "]: " + this.message + ">";
        }

        public boolean isInvalidAuthentication() {
            return httpStatusCode == HttpStatus.SC_UNAUTHORIZED;
        }

        public boolean isNotFound() {
            return httpStatusCode == HttpStatus.SC_NOT_FOUND;
        }

        public boolean isGone() {
            return httpStatusCode == HttpStatus.SC_GONE;
        }

        @Override
        public boolean isTransientError() {
            return httpStatusCode >= 500;
        }
    }

    public static class AutopushClientMalformedResponseException extends AutopushClientRemoteException {
        private static final long serialVersionUID = 2209313149952001909L;

        public AutopushClientMalformedResponseException(HttpResponse response) {
            super(response, 0, 999, "Response malformed", "Response malformed", new ExtendedJSONObject());
        }
    }
}