Skip to content

Commit 869b553

Browse files
committed
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 eae314f commit 869b553

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +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")
44-
.put("RESOURCE_EXHAUSTED", "resource-exhausted")
4542
.build();
4643

4744
private final int successCount;
@@ -105,7 +102,7 @@ private Error(int index, String reason) {
105102
if (reason == null || reason.trim().isEmpty()) {
106103
this.reason = UNKNOWN_ERROR;
107104
} else {
108-
this.reason = ERROR_CODES.getOrDefault(reason, reason);
105+
this.reason = ERROR_CODES.getOrDefault(reason, reason.toLowerCase().replace('_', '-'));
109106
}
110107
}
111108

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ public void testTopicManagementResponseErrorToString() {
415415

416416
@Test
417417
public void testTopicManagementResponseErrorNotInErrorCodes() {
418-
String myError = "myError";
418+
String myError = "MY_ERROR";
419419
GenericJson json = new GenericJson().set("error", myError);
420420
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);
421421

422422
TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
423423

424-
String expected = "[Error{index=0, reason=" + myError + "}]";
424+
String expected = "[Error{index=0, reason=my-error}]";
425425
assertEquals(expected, topicManagementResponse.getErrors().toString());
426426
}
427427

@@ -447,6 +447,18 @@ public void testTopicManagementResponseErrorResourceExhausted() {
447447
assertEquals(expected, topicManagementResponse.getErrors().toString());
448448
}
449449

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+
450462
private static InstanceIdClientImpl initInstanceIdClient(
451463
final MockLowLevelHttpResponse mockResponse,
452464
final HttpResponseInterceptor interceptor) {
@@ -466,7 +478,7 @@ private void checkTopicManagementRequest(
466478
assertEquals(1, result.getFailureCount());
467479
assertEquals(1, result.getErrors().size());
468480
assertEquals(1, result.getErrors().get(0).getIndex());
469-
assertEquals("error_reason", result.getErrors().get(0).getReason());
481+
assertEquals("error-reason", result.getErrors().get(0).getReason());
470482

471483
ByteArrayOutputStream out = new ByteArrayOutputStream();
472484
request.getContent().writeTo(out);

0 commit comments

Comments
 (0)