Skip to content

Commit 814bc6d

Browse files
committed
If the reason is not in the predefined ERROR_CODES it would be more helpful to use the reason String that came in as an parameter than UNKNOWN_ERROR. UNKNOWN_ERROR could be used for null or empty Strings.
1 parent e390198 commit 814bc6d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/main/java/com/google/firebase/messaging/TopicManagementResponse.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ public static class Error {
102102

103103
private Error(int index, String reason) {
104104
this.index = index;
105-
this.reason = ERROR_CODES.containsKey(reason)
106-
? ERROR_CODES.get(reason) : UNKNOWN_ERROR;
105+
if (reason == null || reason.trim().isEmpty()) {
106+
this.reason = UNKNOWN_ERROR;
107+
} else {
108+
this.reason = ERROR_CODES.getOrDefault(reason, reason);
109+
}
107110
}
108111

109112
/**

src/test/java/com/google/firebase/messaging/InstanceIdClientImplTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,30 @@ public void testTopicManagementResponseWithEmptyList() {
404404

405405
@Test
406406
public void testTopicManagementResponseErrorToString() {
407-
GenericJson json = new GenericJson().set("error", "test error");
407+
GenericJson json = new GenericJson().set("error", "INVALID_ARGUMENT");
408+
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
409+
410+
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
411+
412+
String expected = "[Error{index=0, reason=invalid-argument}]";
413+
assertEquals(expected, topicManagementResponse.getErrors().toString());
414+
}
415+
416+
@Test
417+
public void testTopicManagementResponseErrorNotInErrorCodes() {
418+
String myError = "myError";
419+
GenericJson json = new GenericJson().set("error", myError);
420+
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
421+
422+
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
423+
424+
String expected = "[Error{index=0, reason=" + myError + "}]";
425+
assertEquals(expected, topicManagementResponse.getErrors().toString());
426+
}
427+
428+
@Test
429+
public void testTopicManagementResponseErrorUnknown() {
430+
GenericJson json = new GenericJson().set("error", "");
408431
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
409432

410433
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
@@ -443,7 +466,7 @@ private void checkTopicManagementRequest(
443466
assertEquals(1, result.getFailureCount());
444467
assertEquals(1, result.getErrors().size());
445468
assertEquals(1, result.getErrors().get(0).getIndex());
446-
assertEquals("unknown-error", result.getErrors().get(0).getReason());
469+
assertEquals("error_reason", result.getErrors().get(0).getReason());
447470

448471
ByteArrayOutputStream out = new ByteArrayOutputStream();
449472
request.getContent().writeTo(out);

0 commit comments

Comments
 (0)