mobile/android/base/background/fxa/FxAccountClientException.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial