|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.background.fxa; |
|
6 |
|
7 import org.mozilla.gecko.R; |
|
8 import org.mozilla.gecko.sync.ExtendedJSONObject; |
|
9 import org.mozilla.gecko.sync.HTTPFailureException; |
|
10 import org.mozilla.gecko.sync.net.SyncStorageResponse; |
|
11 |
|
12 import ch.boye.httpclientandroidlib.HttpResponse; |
|
13 import ch.boye.httpclientandroidlib.HttpStatus; |
|
14 |
|
15 /** |
|
16 * From <a href="https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md">https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md</a>. |
|
17 */ |
|
18 public class FxAccountClientException extends Exception { |
|
19 private static final long serialVersionUID = 7953459541558266597L; |
|
20 |
|
21 public FxAccountClientException(String detailMessage) { |
|
22 super(detailMessage); |
|
23 } |
|
24 |
|
25 public FxAccountClientException(Exception e) { |
|
26 super(e); |
|
27 } |
|
28 |
|
29 public static class FxAccountClientRemoteException extends FxAccountClientException { |
|
30 private static final long serialVersionUID = 2209313149952001097L; |
|
31 |
|
32 public final HttpResponse response; |
|
33 public final long httpStatusCode; |
|
34 public final long apiErrorNumber; |
|
35 public final String error; |
|
36 public final String message; |
|
37 public final String info; |
|
38 public final ExtendedJSONObject body; |
|
39 |
|
40 public FxAccountClientRemoteException(HttpResponse response, long httpStatusCode, long apiErrorNumber, String error, String message, String info, ExtendedJSONObject body) { |
|
41 super(new HTTPFailureException(new SyncStorageResponse(response))); |
|
42 if (body == null) { |
|
43 throw new IllegalArgumentException("body must not be null"); |
|
44 } |
|
45 this.response = response; |
|
46 this.httpStatusCode = httpStatusCode; |
|
47 this.apiErrorNumber = apiErrorNumber; |
|
48 this.error = error; |
|
49 this.message = message; |
|
50 this.info = info; |
|
51 this.body = body; |
|
52 } |
|
53 |
|
54 @Override |
|
55 public String toString() { |
|
56 return "<FxAccountClientRemoteException " + this.httpStatusCode + " [" + this.apiErrorNumber + "]: " + this.message + ">"; |
|
57 } |
|
58 |
|
59 public boolean isInvalidAuthentication() { |
|
60 return httpStatusCode == HttpStatus.SC_UNAUTHORIZED; |
|
61 } |
|
62 |
|
63 public boolean isAccountAlreadyExists() { |
|
64 return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_CREATE_AN_ACCOUNT_THAT_ALREADY_EXISTS; |
|
65 } |
|
66 |
|
67 public boolean isAccountDoesNotExist() { |
|
68 return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_ACCESS_AN_ACCOUNT_THAT_DOES_NOT_EXIST; |
|
69 } |
|
70 |
|
71 public boolean isBadPassword() { |
|
72 return apiErrorNumber == FxAccountRemoteError.INCORRECT_PASSWORD; |
|
73 } |
|
74 |
|
75 public boolean isUnverified() { |
|
76 return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_OPERATE_ON_AN_UNVERIFIED_ACCOUNT; |
|
77 } |
|
78 |
|
79 public boolean isUpgradeRequired() { |
|
80 return |
|
81 apiErrorNumber == FxAccountRemoteError.ENDPOINT_IS_NO_LONGER_SUPPORTED || |
|
82 apiErrorNumber == FxAccountRemoteError.INCORRECT_LOGIN_METHOD_FOR_THIS_ACCOUNT || |
|
83 apiErrorNumber == FxAccountRemoteError.INCORRECT_KEY_RETRIEVAL_METHOD_FOR_THIS_ACCOUNT || |
|
84 apiErrorNumber == FxAccountRemoteError.INCORRECT_API_VERSION_FOR_THIS_ACCOUNT; |
|
85 } |
|
86 |
|
87 public boolean isTooManyRequests() { |
|
88 return apiErrorNumber == FxAccountRemoteError.CLIENT_HAS_SENT_TOO_MANY_REQUESTS; |
|
89 } |
|
90 |
|
91 public boolean isServerUnavailable() { |
|
92 return apiErrorNumber == FxAccountRemoteError.SERVICE_TEMPORARILY_UNAVAILABLE_DUE_TO_HIGH_LOAD; |
|
93 } |
|
94 |
|
95 public boolean isBadEmailCase() { |
|
96 return apiErrorNumber == FxAccountRemoteError.INCORRECT_EMAIL_CASE; |
|
97 } |
|
98 |
|
99 public int getErrorMessageStringResource() { |
|
100 if (isUpgradeRequired()) { |
|
101 return R.string.fxaccount_remote_error_UPGRADE_REQUIRED; |
|
102 } else if (isAccountAlreadyExists()) { |
|
103 return R.string.fxaccount_remote_error_ATTEMPT_TO_CREATE_AN_ACCOUNT_THAT_ALREADY_EXISTS; |
|
104 } else if (isAccountDoesNotExist()) { |
|
105 return R.string.fxaccount_remote_error_ATTEMPT_TO_ACCESS_AN_ACCOUNT_THAT_DOES_NOT_EXIST; |
|
106 } else if (isBadPassword()) { |
|
107 return R.string.fxaccount_remote_error_INCORRECT_PASSWORD; |
|
108 } else if (isUnverified()) { |
|
109 return R.string.fxaccount_remote_error_ATTEMPT_TO_OPERATE_ON_AN_UNVERIFIED_ACCOUNT; |
|
110 } else if (isTooManyRequests()) { |
|
111 return R.string.fxaccount_remote_error_CLIENT_HAS_SENT_TOO_MANY_REQUESTS; |
|
112 } else if (isServerUnavailable()) { |
|
113 return R.string.fxaccount_remote_error_SERVICE_TEMPORARILY_UNAVAILABLE_TO_DUE_HIGH_LOAD; |
|
114 } else { |
|
115 return R.string.fxaccount_remote_error_UNKNOWN_ERROR; |
|
116 } |
|
117 } |
|
118 } |
|
119 |
|
120 public static class FxAccountClientMalformedResponseException extends FxAccountClientRemoteException { |
|
121 private static final long serialVersionUID = 2209313149952001098L; |
|
122 |
|
123 public FxAccountClientMalformedResponseException(HttpResponse response) { |
|
124 super(response, 0, FxAccountRemoteError.UNKNOWN_ERROR, "Response malformed", "Response malformed", "Response malformed", new ExtendedJSONObject()); |
|
125 } |
|
126 } |
|
127 } |