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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/fxa/FxAccountClientException.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.background.fxa;
     1.9 +
    1.10 +import org.mozilla.gecko.R;
    1.11 +import org.mozilla.gecko.sync.ExtendedJSONObject;
    1.12 +import org.mozilla.gecko.sync.HTTPFailureException;
    1.13 +import org.mozilla.gecko.sync.net.SyncStorageResponse;
    1.14 +
    1.15 +import ch.boye.httpclientandroidlib.HttpResponse;
    1.16 +import ch.boye.httpclientandroidlib.HttpStatus;
    1.17 +
    1.18 +/**
    1.19 + * 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>.
    1.20 + */
    1.21 +public class FxAccountClientException extends Exception {
    1.22 +  private static final long serialVersionUID = 7953459541558266597L;
    1.23 +
    1.24 +  public FxAccountClientException(String detailMessage) {
    1.25 +    super(detailMessage);
    1.26 +  }
    1.27 +
    1.28 +  public FxAccountClientException(Exception e) {
    1.29 +    super(e);
    1.30 +  }
    1.31 +
    1.32 +  public static class FxAccountClientRemoteException extends FxAccountClientException {
    1.33 +    private static final long serialVersionUID = 2209313149952001097L;
    1.34 +
    1.35 +    public final HttpResponse response;
    1.36 +    public final long httpStatusCode;
    1.37 +    public final long apiErrorNumber;
    1.38 +    public final String error;
    1.39 +    public final String message;
    1.40 +    public final String info;
    1.41 +    public final ExtendedJSONObject body;
    1.42 +
    1.43 +    public FxAccountClientRemoteException(HttpResponse response, long httpStatusCode, long apiErrorNumber, String error, String message, String info, ExtendedJSONObject body) {
    1.44 +      super(new HTTPFailureException(new SyncStorageResponse(response)));
    1.45 +      if (body == null) {
    1.46 +        throw new IllegalArgumentException("body must not be null");
    1.47 +      }
    1.48 +      this.response = response;
    1.49 +      this.httpStatusCode = httpStatusCode;
    1.50 +      this.apiErrorNumber = apiErrorNumber;
    1.51 +      this.error = error;
    1.52 +      this.message = message;
    1.53 +      this.info = info;
    1.54 +      this.body = body;
    1.55 +    }
    1.56 +
    1.57 +    @Override
    1.58 +    public String toString() {
    1.59 +      return "<FxAccountClientRemoteException " + this.httpStatusCode + " [" + this.apiErrorNumber + "]: " + this.message + ">";
    1.60 +    }
    1.61 +
    1.62 +    public boolean isInvalidAuthentication() {
    1.63 +      return httpStatusCode == HttpStatus.SC_UNAUTHORIZED;
    1.64 +    }
    1.65 +
    1.66 +    public boolean isAccountAlreadyExists() {
    1.67 +      return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_CREATE_AN_ACCOUNT_THAT_ALREADY_EXISTS;
    1.68 +    }
    1.69 +
    1.70 +    public boolean isAccountDoesNotExist() {
    1.71 +      return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_ACCESS_AN_ACCOUNT_THAT_DOES_NOT_EXIST;
    1.72 +    }
    1.73 +
    1.74 +    public boolean isBadPassword() {
    1.75 +      return apiErrorNumber == FxAccountRemoteError.INCORRECT_PASSWORD;
    1.76 +    }
    1.77 +
    1.78 +    public boolean isUnverified() {
    1.79 +      return apiErrorNumber == FxAccountRemoteError.ATTEMPT_TO_OPERATE_ON_AN_UNVERIFIED_ACCOUNT;
    1.80 +    }
    1.81 +
    1.82 +    public boolean isUpgradeRequired() {
    1.83 +      return
    1.84 +          apiErrorNumber == FxAccountRemoteError.ENDPOINT_IS_NO_LONGER_SUPPORTED ||
    1.85 +          apiErrorNumber == FxAccountRemoteError.INCORRECT_LOGIN_METHOD_FOR_THIS_ACCOUNT ||
    1.86 +          apiErrorNumber == FxAccountRemoteError.INCORRECT_KEY_RETRIEVAL_METHOD_FOR_THIS_ACCOUNT ||
    1.87 +          apiErrorNumber == FxAccountRemoteError.INCORRECT_API_VERSION_FOR_THIS_ACCOUNT;
    1.88 +    }
    1.89 +
    1.90 +    public boolean isTooManyRequests() {
    1.91 +      return apiErrorNumber == FxAccountRemoteError.CLIENT_HAS_SENT_TOO_MANY_REQUESTS;
    1.92 +    }
    1.93 +
    1.94 +    public boolean isServerUnavailable() {
    1.95 +      return apiErrorNumber == FxAccountRemoteError.SERVICE_TEMPORARILY_UNAVAILABLE_DUE_TO_HIGH_LOAD;
    1.96 +    }
    1.97 +
    1.98 +    public boolean isBadEmailCase() {
    1.99 +      return apiErrorNumber == FxAccountRemoteError.INCORRECT_EMAIL_CASE;
   1.100 +    }
   1.101 +
   1.102 +    public int getErrorMessageStringResource() {
   1.103 +      if (isUpgradeRequired()) {
   1.104 +        return R.string.fxaccount_remote_error_UPGRADE_REQUIRED;
   1.105 +      } else if (isAccountAlreadyExists()) {
   1.106 +        return R.string.fxaccount_remote_error_ATTEMPT_TO_CREATE_AN_ACCOUNT_THAT_ALREADY_EXISTS;
   1.107 +      } else if (isAccountDoesNotExist()) {
   1.108 +        return R.string.fxaccount_remote_error_ATTEMPT_TO_ACCESS_AN_ACCOUNT_THAT_DOES_NOT_EXIST;
   1.109 +      } else if (isBadPassword()) {
   1.110 +        return R.string.fxaccount_remote_error_INCORRECT_PASSWORD;
   1.111 +      } else if (isUnverified()) {
   1.112 +        return R.string.fxaccount_remote_error_ATTEMPT_TO_OPERATE_ON_AN_UNVERIFIED_ACCOUNT;
   1.113 +      } else if (isTooManyRequests()) {
   1.114 +        return R.string.fxaccount_remote_error_CLIENT_HAS_SENT_TOO_MANY_REQUESTS;
   1.115 +      } else if (isServerUnavailable()) {
   1.116 +        return R.string.fxaccount_remote_error_SERVICE_TEMPORARILY_UNAVAILABLE_TO_DUE_HIGH_LOAD;
   1.117 +      } else {
   1.118 +        return R.string.fxaccount_remote_error_UNKNOWN_ERROR;
   1.119 +      }
   1.120 +    }
   1.121 +  }
   1.122 +
   1.123 +  public static class FxAccountClientMalformedResponseException extends FxAccountClientRemoteException {
   1.124 +    private static final long serialVersionUID = 2209313149952001098L;
   1.125 +
   1.126 +    public FxAccountClientMalformedResponseException(HttpResponse response) {
   1.127 +      super(response, 0, FxAccountRemoteError.UNKNOWN_ERROR, "Response malformed", "Response malformed", "Response malformed", new ExtendedJSONObject());
   1.128 +    }
   1.129 +  }
   1.130 +}

mercurial