Skip to content

Commit be03460

Browse files
authored
Handling FCM canonical error codes (firebase#148)
* Handling FCM canonical error codes * Added PERMISSION_DENIED error code
1 parent a2ba8bc commit be03460

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Unreleased
22

3-
-
3+
- [fixed] Improved error handling in FCM by mapping more server-side
4+
errors to client-side error codes.
45

56
# v5.9.0
67

7-
88
### Cloud Messaging
99

1010
- [feature] Added the `FirebaseCloudMessaging` API for sending

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,20 @@ public class FirebaseMessaging {
6363
private static final String UNKNOWN_ERROR = "unknown-error";
6464
private static final Map<String, String> FCM_ERROR_CODES =
6565
ImmutableMap.<String, String>builder()
66-
.put("APNS_AUTH_ERROR", "authentication-error")
66+
// FCM v1 canonical error codes
67+
.put("NOT_FOUND", "registration-token-not-registered")
68+
.put("PERMISSION_DENIED", "mismatched-credential")
69+
.put("RESOURCE_EXHAUSTED", "message-rate-exceeded")
70+
.put("UNAUTHENTICATED", "invalid-apns-credentials")
71+
72+
// FCM v1 new error codes
73+
.put("APNS_AUTH_ERROR", "invalid-apns-credentials")
6774
.put("INTERNAL", INTERNAL_ERROR)
6875
.put("INVALID_ARGUMENT", "invalid-argument")
69-
.put("UNREGISTERED", "registration-token-not-registered")
7076
.put("QUOTA_EXCEEDED", "message-rate-exceeded")
71-
.put("SENDER_ID_MISMATCH", "authentication-error")
77+
.put("SENDER_ID_MISMATCH", "mismatched-credential")
7278
.put("UNAVAILABLE", "server-unavailable")
79+
.put("UNREGISTERED", "registration-token-not-registered")
7380
.build();
7481
static final Map<Integer, String> IID_ERROR_CODES =
7582
ImmutableMap.<Integer, String>builder()

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ public void testSendErrorWithDetails() throws Exception {
195195
}
196196
}
197197

198+
@Test
199+
public void testSendErrorWithCanonicalCode() throws Exception {
200+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
201+
FirebaseMessaging messaging = initMessaging(response);
202+
for (int code : HTTP_ERRORS) {
203+
response.setStatusCode(code).setContent(
204+
"{\"error\": {\"status\": \"NOT_FOUND\", \"message\": \"test error\"}}");
205+
TestResponseInterceptor interceptor = new TestResponseInterceptor();
206+
messaging.setInterceptor(interceptor);
207+
try {
208+
messaging.sendAsync(Message.builder().setTopic("test-topic").build()).get();
209+
fail("No error thrown for HTTP error");
210+
} catch (ExecutionException e) {
211+
assertTrue(e.getCause() instanceof FirebaseMessagingException);
212+
FirebaseMessagingException error = (FirebaseMessagingException) e.getCause();
213+
assertEquals("registration-token-not-registered", error.getErrorCode());
214+
assertEquals("test error", error.getMessage());
215+
assertTrue(error.getCause() instanceof HttpResponseException);
216+
}
217+
checkRequestHeader(interceptor);
218+
}
219+
}
220+
198221
@Test
199222
public void testInvalidSubscribe() {
200223
FirebaseMessaging messaging = initDefaultMessaging();

0 commit comments

Comments
 (0)