Skip to content

Commit bf783c3

Browse files
authored
Handling FCM canonical error codes (#131)
1 parent 432a207 commit bf783c3

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

CHANGELOG.md

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

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

56
# v2.9.0
67

firebase_admin/messaging.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,18 @@ class _MessagingService(object):
716716
INTERNAL_ERROR = 'internal-error'
717717
UNKNOWN_ERROR = 'unknown-error'
718718
FCM_ERROR_CODES = {
719-
'APNS_AUTH_ERROR': 'authentication-error',
719+
# FCM v1 canonical error codes
720+
'NOT_FOUND': 'registration-token-not-registered',
721+
'PERMISSION_DENIED': 'mismatched-credential',
722+
'RESOURCE_EXHAUSTED': 'message-rate-exceeded',
723+
'UNAUTHENTICATED': 'invalid-apns-credentials',
724+
725+
# FCM v1 new error codes
726+
'APNS_AUTH_ERROR': 'invalid-apns-credentials',
720727
'INTERNAL': INTERNAL_ERROR,
721728
'INVALID_ARGUMENT': 'invalid-argument',
722729
'QUOTA_EXCEEDED': 'message-rate-exceeded',
723-
'SENDER_ID_MISMATCH': 'authentication-error',
730+
'SENDER_ID_MISMATCH': 'mismatched-credential',
724731
'UNAVAILABLE': 'server-unavailable',
725732
'UNREGISTERED': 'registration-token-not-registered',
726733
}

tests/test_messaging.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,26 @@ def test_send_detailed_error(self, status):
855855
body = {'message': messaging._MessagingService.JSON_ENCODER.default(msg)}
856856
assert json.loads(recorder[0].body.decode()) == body
857857

858+
@pytest.mark.parametrize('status', HTTP_ERRORS)
859+
def test_send_canonical_error_code(self, status):
860+
payload = json.dumps({
861+
'error': {
862+
'status': 'NOT_FOUND',
863+
'message': 'test error'
864+
}
865+
})
866+
_, recorder = self._instrument_messaging_service(status=status, payload=payload)
867+
msg = messaging.Message(topic='foo')
868+
with pytest.raises(messaging.ApiCallError) as excinfo:
869+
messaging.send(msg)
870+
assert str(excinfo.value) == 'test error'
871+
assert str(excinfo.value.code) == 'registration-token-not-registered'
872+
assert len(recorder) == 1
873+
assert recorder[0].method == 'POST'
874+
assert recorder[0].url == self._get_url('explicit-project-id')
875+
body = {'message': messaging._MessagingService.JSON_ENCODER.default(msg)}
876+
assert json.loads(recorder[0].body.decode()) == body
877+
858878

859879
class TestTopicManagement(object):
860880

0 commit comments

Comments
 (0)