michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.net; michael@0: michael@0: import java.io.IOException; michael@0: import java.util.HashMap; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: michael@0: public class SyncStorageResponse extends SyncResponse { michael@0: michael@0: // Responses that are actionable get constant status codes. michael@0: public static final String RESPONSE_CLIENT_UPGRADE_REQUIRED = "16"; michael@0: michael@0: private static final String LOG_TAG = "SyncStorageResponse"; michael@0: public static HashMap SERVER_ERROR_MESSAGES; michael@0: static { michael@0: HashMap errors = new HashMap(); michael@0: michael@0: // Sync protocol errors. michael@0: errors.put("1", "Illegal method/protocol"); michael@0: errors.put("2", "Incorrect/missing CAPTCHA"); michael@0: errors.put("3", "Invalid/missing username"); michael@0: errors.put("4", "Attempt to overwrite data that can't be overwritten (such as creating a user ID that already exists)"); michael@0: errors.put("5", "User ID does not match account in path"); michael@0: errors.put("6", "JSON parse failure"); michael@0: errors.put("7", "Missing password field"); michael@0: errors.put("8", "Invalid Weave Basic Object"); michael@0: errors.put("9", "Requested password not strong enough"); michael@0: errors.put("10", "Invalid/missing password reset code"); michael@0: errors.put("11", "Unsupported function"); michael@0: errors.put("12", "No email address on file"); michael@0: errors.put("13", "Invalid collection"); michael@0: errors.put("14", "User over quota"); michael@0: errors.put("15", "The email does not match the username"); michael@0: errors.put(RESPONSE_CLIENT_UPGRADE_REQUIRED, "Client upgrade required"); michael@0: errors.put("255", "An unexpected server error occurred: pool is empty."); michael@0: michael@0: // Infrastructure-generated errors. michael@0: errors.put("\"server issue: getVS failed\"", "server issue: getVS failed"); michael@0: errors.put("\"server issue: prefix not set\"", "server issue: prefix not set"); michael@0: errors.put("\"server issue: host header not received from client\"", "server issue: host header not received from client"); michael@0: errors.put("\"server issue: database lookup failed\"", "server issue: database lookup failed"); michael@0: errors.put("\"server issue: database is not healthy\"", "server issue: database is not healthy"); michael@0: errors.put("\"server issue: database not in pool\"", "server issue: database not in pool"); michael@0: errors.put("\"server issue: database marked as down\"", "server issue: database marked as down"); michael@0: SERVER_ERROR_MESSAGES = errors; michael@0: } michael@0: public static String getServerErrorMessage(String body) { michael@0: Logger.debug(LOG_TAG, "Looking up message for body \"" + body + "\""); michael@0: if (SERVER_ERROR_MESSAGES.containsKey(body)) { michael@0: return SERVER_ERROR_MESSAGES.get(body); michael@0: } michael@0: return body; michael@0: } michael@0: michael@0: michael@0: public SyncStorageResponse(HttpResponse res) { michael@0: this.response = res; michael@0: } michael@0: michael@0: public String getErrorMessage() throws IllegalStateException, IOException { michael@0: return SyncStorageResponse.getServerErrorMessage(this.body().trim()); michael@0: } michael@0: michael@0: // TODO: Content-Type and Content-Length validation. michael@0: michael@0: }