Repository: firebase/firebase-admin-java
Pull request #381 (2020-03-10 20:51:25)

Handling IID error codes correctly
IID error responses do not contain a message, only an error code.


Patch:
diff --git a/src/main/java/com/google/firebase/messaging/InstanceIdClientImpl.java b/src/main/java/com/google/firebase/messaging/InstanceIdClientImpl.java
index 09962a4..81e953f 100644
--- a/src/main/java/com/google/firebase/messaging/InstanceIdClientImpl.java
+++ b/src/main/java/com/google/firebase/messaging/InstanceIdClientImpl.java
@@ -147,7 +147,7 @@ final class InstanceIdClientImpl implements InstanceIdClient {
       String response = getResponse(base);
       InstanceIdServiceErrorResponse parsed = safeParse(response);
       if (!Strings.isNullOrEmpty(parsed.error)) {
-        return parsed.error;
+        return "Error while calling the IID service: " + parsed.error;
       }
 
       return base.getMessage();


Test patch:
diff --git a/src/test/java/com/google/firebase/messaging/InstanceIdClientImplTest.java b/src/test/java/com/google/firebase/messaging/InstanceIdClientImplTest.java
index 76907db..bfbc881 100644
--- a/src/test/java/com/google/firebase/messaging/InstanceIdClientImplTest.java
+++ b/src/test/java/com/google/firebase/messaging/InstanceIdClientImplTest.java
@@ -109,14 +109,16 @@ public class InstanceIdClientImplTest {
     TestResponseInterceptor interceptor = new TestResponseInterceptor();
     InstanceIdClient client = initInstanceIdClient(response, interceptor);
 
+    String content = "{\"error\": \"ErrorCode\"}";
     for (int statusCode : HTTP_ERRORS) {
-      response.setStatusCode(statusCode).setContent("{\"error\": \"test error\"}");
+      response.setStatusCode(statusCode).setContent(content);
 
       try {
         client.subscribeToTopic("test-topic", ImmutableList.of("id1", "id2"));
         fail("No error thrown for HTTP error");
       } catch (FirebaseMessagingException error) {
-        checkExceptionFromHttpResponse(error, statusCode, "test error");
+        String expectedMessage = "Error while calling the IID service: ErrorCode";
+        checkExceptionFromHttpResponse(error, statusCode, expectedMessage);
       }
 
       checkTopicManagementRequestHeader(interceptor.getLastRequest(), TEST_IID_SUBSCRIBE_URL);
@@ -247,14 +249,16 @@ public class InstanceIdClientImplTest {
     TestResponseInterceptor interceptor = new TestResponseInterceptor();
     InstanceIdClient client = initInstanceIdClient(response, interceptor);
 
+    String content = "{\"error\": \"ErrorCode\"}";
     for (int statusCode : HTTP_ERRORS) {
-      response.setStatusCode(statusCode).setContent("{\"error\": \"test error\"}");
+      response.setStatusCode(statusCode).setContent(content);
 
       try {
         client.unsubscribeFromTopic("test-topic", ImmutableList.of("id1", "id2"));
         fail("No error thrown for HTTP error");
       } catch (FirebaseMessagingException error) {
-        checkExceptionFromHttpResponse(error, statusCode, "test error");
+        String expectedMessage = "Error while calling the IID service: ErrorCode";
+        checkExceptionFromHttpResponse(error, statusCode, expectedMessage);
       }
 
       checkTopicManagementRequestHeader(interceptor.getLastRequest(), TEST_IID_UNSUBSCRIBE_URL);