diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b2583f7..8e0ff764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- [changed] Improved error handling in FCM by mapping more server-side + errors to client-side error codes. - [added] Added the `db` package for interacting with the Firebase database. # v2.5.0 diff --git a/messaging/messaging.go b/messaging/messaging.go index a3d71e97..231e7212 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -41,13 +41,20 @@ var ( topicNamePattern = regexp.MustCompile("^(/topics/)?(private/)?[a-zA-Z0-9-_.~%]+$") fcmErrorCodes = map[string]string{ + // FCM v1 canonical error codes + "NOT_FOUND": "app instance has been unregistered; code: registration-token-not-registered", + "PERMISSION_DENIED": "sender id does not match regisration token; code: mismatched-credential", + "RESOURCE_EXHAUSTED": "messaging service quota exceeded; code: message-rate-exceeded", + "UNAUTHENTICATED": "apns certificate or auth key was invalid; code: invalid-apns-credentials", + + // FCM v1 new error codes + "APNS_AUTH_ERROR": "apns certificate or auth key was invalid; code: invalid-apns-credentials", + "INTERNAL": "back servers encountered an unknown internl error; code: internal-error", "INVALID_ARGUMENT": "request contains an invalid argument; code: invalid-argument", - "UNREGISTERED": "app instance has been unregistered; code: registration-token-not-registered", - "SENDER_ID_MISMATCH": "sender id does not match regisration token; code: authentication-error", + "SENDER_ID_MISMATCH": "sender id does not match regisration token; code: mismatched-credential", "QUOTA_EXCEEDED": "messaging service quota exceeded; code: message-rate-exceeded", - "APNS_AUTH_ERROR": "apns certificate or auth key was invalid; code: authentication-error", "UNAVAILABLE": "backend servers are temporarily unavailable; code: server-unavailable", - "INTERNAL": "back servers encountered an unknown internl error; code: internal-error", + "UNREGISTERED": "app instance has been unregistered; code: registration-token-not-registered", } iidErrorCodes = map[string]string{ diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index 53b0650a..79ef498b 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -632,6 +632,10 @@ func TestSendError(t *testing.T) { resp: "{\"error\": {\"status\": \"INVALID_ARGUMENT\", \"message\": \"test error\"}}", want: "http error status: 500; reason: request contains an invalid argument; code: invalid-argument", }, + { + resp: "{\"error\": {\"status\": \"NOT_FOUND\", \"message\": \"test error\"}}", + want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered", + }, { resp: "not json", want: "http error status: 500; reason: server responded with an unknown error; response: not json",