Bug 1261995 - Quiet infer NullPointerException warnings. r?mcomella draft
authorNick Alexander <nalexander@mozilla.com>
Tue, 05 Apr 2016 16:32:01 -0700
changeset 347844 6f36a3110086453834ec269f83887b2ad5d8d846
parent 347843 bc199c0f7d31d8079f7236a24f86ab99c7bbcb49
child 517730 830682a2e1fa873a1217c32e8df25d11f47b9f8d
push id14690
push usernalexander@mozilla.com
push dateTue, 05 Apr 2016 23:32:19 +0000
reviewersmcomella
bugs1261995
milestone48.0a1
Bug 1261995 - Quiet infer NullPointerException warnings. r?mcomella MozReview-Commit-ID: C0LPj4kGbPk
mobile/android/services/src/main/java/org/mozilla/gecko/push/autopush/AutopushClient.java
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/push/autopush/AutopushClient.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/push/autopush/AutopushClient.java
@@ -55,16 +55,19 @@ public class AutopushClient {
     protected static final String[] REGISTER_USER_AGENT_RESPONSE_REQUIRED_STRING_FIELDS = new String[] { JSON_KEY_UAID, JSON_KEY_SECRET, JSON_KEY_CHANNEL_ID, JSON_KEY_ENDPOINT };
     protected static final String[] REGISTER_CHANNEL_RESPONSE_REQUIRED_STRING_FIELDS = new String[] { JSON_KEY_CHANNEL_ID, JSON_KEY_ENDPOINT };
 
     public static final String JSON_KEY_CODE = "code";
     public static final String JSON_KEY_ERRNO = "errno";
     public static final String JSON_KEY_ERROR = "error";
     public static final String JSON_KEY_MESSAGE = "message";
 
+    protected static final String[] requiredErrorStringFields = { JSON_KEY_ERROR, JSON_KEY_MESSAGE };
+    protected static final String[] requiredErrorLongFields = { JSON_KEY_CODE, JSON_KEY_ERRNO };
+
     /**
      * The server's URI.
      * <p>
      * We assume throughout that this ends with a trailing slash (and guarantee as
      * much in the constructor).
      */
     public final String serverURI;
 
@@ -131,29 +134,30 @@ public class AutopushClient {
      * @return response's HTTP status code.
      * @throws AutopushClientException
      */
     public static int validateResponse(HttpResponse response) throws AutopushClientException {
         final int status = response.getStatusLine().getStatusCode();
         if (200 <= status && status <= 299) {
             return status;
         }
-        int code;
-        int errno;
+        long code;
+        long errno;
         String error;
         String message;
         String info;
         ExtendedJSONObject body;
         try {
             body = new SyncStorageResponse(response).jsonObjectBody();
             // TODO: The service doesn't do the right thing yet :(
             // body.throwIfFieldsMissingOrMisTyped(requiredErrorStringFields, String.class);
-            // body.throwIfFieldsMissingOrMisTyped(requiredErrorLongFields, Long.class);
-            code = body.getLong(JSON_KEY_CODE).intValue();
-            errno = body.getLong(JSON_KEY_ERRNO).intValue();
+            body.throwIfFieldsMissingOrMisTyped(requiredErrorLongFields, Long.class);
+            // Would throw above if missing; the -1 defaults quiet NPE warnings.
+            code = body.getLong(JSON_KEY_CODE, -1);
+            errno = body.getLong(JSON_KEY_ERRNO, -1);
             error = body.getString(JSON_KEY_ERROR);
             message = body.getString(JSON_KEY_MESSAGE);
         } catch (Exception e) {
             throw new AutopushClientException.AutopushClientMalformedResponseException(response);
         }
         throw new AutopushClientException.AutopushClientRemoteException(response, code, errno, error, message, body);
     }