Skip to content

Commit 88381d8

Browse files
authored
fix(fcm): Preserve unmapped TopicManagementResponse error reasons (#1073)
* RESOURCE_EXHAUSTED is a possible error code as well, given https://developers.google.com/instance-id/reference/server * 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. * For consistency with the current format of the error reasons, logic is added to map them from UPPER_SNAKE_CASE to lower-kebab-case. Entries from the ERROR_CODES that can be computed this way are removed from the map. Extra tests are added for those 3 cases.
1 parent 1e3bddc commit 88381d8

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public class TopicManagementResponse {
3737
// Server error codes as defined in https://developers.google.com/instance-id/reference/server
3838
// TODO: Should we handle other error codes here (e.g. PERMISSION_DENIED)?
3939
private static final Map<String, String> ERROR_CODES = ImmutableMap.<String, String>builder()
40-
.put("INVALID_ARGUMENT", "invalid-argument")
4140
.put("NOT_FOUND", "registration-token-not-registered")
4241
.put("INTERNAL", "internal-error")
43-
.put("TOO_MANY_TOPICS", "too-many-topics")
4442
.build();
4543

4644
private final int successCount;
@@ -101,8 +99,11 @@ public static class Error {
10199

102100
private Error(int index, String reason) {
103101
this.index = index;
104-
this.reason = ERROR_CODES.containsKey(reason)
105-
? ERROR_CODES.get(reason) : UNKNOWN_ERROR;
102+
if (reason == null || reason.trim().isEmpty()) {
103+
this.reason = UNKNOWN_ERROR;
104+
} else {
105+
this.reason = ERROR_CODES.getOrDefault(reason, reason.toLowerCase().replace('_', '-'));
106+
}
106107
}
107108

108109
/**

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

Lines changed: 48 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 = "MY_ERROR";
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=my-error}]";
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);
@@ -413,6 +436,29 @@ public void testTopicManagementResponseErrorToString() {
413436
assertEquals(expected, topicManagementResponse.getErrors().toString());
414437
}
415438

439+
@Test
440+
public void testTopicManagementResponseErrorResourceExhausted() {
441+
GenericJson json = new GenericJson().set("error", "RESOURCE_EXHAUSTED");
442+
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
443+
444+
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
445+
446+
String expected = "[Error{index=0, reason=resource-exhausted}]";
447+
assertEquals(expected, topicManagementResponse.getErrors().toString());
448+
}
449+
450+
@Test
451+
public void testTopicManagementResponseErrorTooManyTopics() {
452+
GenericJson json = new GenericJson().set("error", "TOO_MANY_TOPICS");
453+
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
454+
455+
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
456+
457+
String expected = "[Error{index=0, reason=too-many-topics}]";
458+
assertEquals(expected, topicManagementResponse.getErrors().toString());
459+
}
460+
461+
416462
private static InstanceIdClientImpl initInstanceIdClient(
417463
final MockLowLevelHttpResponse mockResponse,
418464
final HttpResponseInterceptor interceptor) {
@@ -432,7 +478,7 @@ private void checkTopicManagementRequest(
432478
assertEquals(1, result.getFailureCount());
433479
assertEquals(1, result.getErrors().size());
434480
assertEquals(1, result.getErrors().get(0).getIndex());
435-
assertEquals("unknown-error", result.getErrors().get(0).getReason());
481+
assertEquals("error-reason", result.getErrors().get(0).getReason());
436482

437483
ByteArrayOutputStream out = new ByteArrayOutputStream();
438484
request.getContent().writeTo(out);

0 commit comments

Comments
 (0)