mobile/android/base/sync/net/SyncStorageResponse.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/net/SyncStorageResponse.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     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.sync.net;
     1.9 +
    1.10 +import java.io.IOException;
    1.11 +import java.util.HashMap;
    1.12 +
    1.13 +import org.mozilla.gecko.background.common.log.Logger;
    1.14 +
    1.15 +import ch.boye.httpclientandroidlib.HttpResponse;
    1.16 +
    1.17 +public class SyncStorageResponse extends SyncResponse {
    1.18 +
    1.19 +  // Responses that are actionable get constant status codes.
    1.20 +  public static final String RESPONSE_CLIENT_UPGRADE_REQUIRED = "16";
    1.21 +
    1.22 +  private static final String LOG_TAG = "SyncStorageResponse";
    1.23 +  public static HashMap<String, String> SERVER_ERROR_MESSAGES;
    1.24 +  static {
    1.25 +    HashMap<String, String> errors = new HashMap<String, String>();
    1.26 +
    1.27 +    // Sync protocol errors.
    1.28 +    errors.put("1", "Illegal method/protocol");
    1.29 +    errors.put("2", "Incorrect/missing CAPTCHA");
    1.30 +    errors.put("3", "Invalid/missing username");
    1.31 +    errors.put("4", "Attempt to overwrite data that can't be overwritten (such as creating a user ID that already exists)");
    1.32 +    errors.put("5", "User ID does not match account in path");
    1.33 +    errors.put("6", "JSON parse failure");
    1.34 +    errors.put("7", "Missing password field");
    1.35 +    errors.put("8", "Invalid Weave Basic Object");
    1.36 +    errors.put("9", "Requested password not strong enough");
    1.37 +    errors.put("10", "Invalid/missing password reset code");
    1.38 +    errors.put("11", "Unsupported function");
    1.39 +    errors.put("12", "No email address on file");
    1.40 +    errors.put("13", "Invalid collection");
    1.41 +    errors.put("14", "User over quota");
    1.42 +    errors.put("15", "The email does not match the username");
    1.43 +    errors.put(RESPONSE_CLIENT_UPGRADE_REQUIRED, "Client upgrade required");
    1.44 +    errors.put("255", "An unexpected server error occurred: pool is empty.");
    1.45 +
    1.46 +    // Infrastructure-generated errors.
    1.47 +    errors.put("\"server issue: getVS failed\"",                         "server issue: getVS failed");
    1.48 +    errors.put("\"server issue: prefix not set\"",                       "server issue: prefix not set");
    1.49 +    errors.put("\"server issue: host header not received from client\"", "server issue: host header not received from client");
    1.50 +    errors.put("\"server issue: database lookup failed\"",               "server issue: database lookup failed");
    1.51 +    errors.put("\"server issue: database is not healthy\"",              "server issue: database is not healthy");
    1.52 +    errors.put("\"server issue: database not in pool\"",                 "server issue: database not in pool");
    1.53 +    errors.put("\"server issue: database marked as down\"",              "server issue: database marked as down");
    1.54 +    SERVER_ERROR_MESSAGES = errors;
    1.55 +  }
    1.56 +  public static String getServerErrorMessage(String body) {
    1.57 +    Logger.debug(LOG_TAG, "Looking up message for body \"" + body + "\"");
    1.58 +    if (SERVER_ERROR_MESSAGES.containsKey(body)) {
    1.59 +      return SERVER_ERROR_MESSAGES.get(body);
    1.60 +    }
    1.61 +    return body;
    1.62 +  }
    1.63 +
    1.64 +
    1.65 +  public SyncStorageResponse(HttpResponse res) {
    1.66 +    this.response = res;
    1.67 +  }
    1.68 +
    1.69 +  public String getErrorMessage() throws IllegalStateException, IOException {
    1.70 +    return SyncStorageResponse.getServerErrorMessage(this.body().trim());
    1.71 +  }
    1.72 +
    1.73 +  // TODO: Content-Type and Content-Length validation.
    1.74 +
    1.75 +}

mercurial