Skip to content

Commit 201a4f3

Browse files
authored
Reading FCM error code from details section (#127)
1 parent 1ad967f commit 201a4f3

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

messaging/messaging.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ type fcmResponse struct {
381381

382382
type fcmError struct {
383383
Error struct {
384-
Status string `json:"status"`
384+
Status string `json:"status"`
385+
Details []struct {
386+
Type string `json:"@type"`
387+
ErrorCode string `json:"errorCode"`
388+
}
385389
} `json:"error"`
386390
}
387391

@@ -422,7 +426,17 @@ func (c *Client) makeSendRequest(ctx context.Context, req *fcmRequest) (string,
422426

423427
var fe fcmError
424428
json.Unmarshal(resp.Body, &fe) // ignore any json parse errors at this level
425-
msg := fcmErrorCodes[fe.Error.Status]
429+
var code string
430+
for _, d := range fe.Error.Details {
431+
if d.Type == "type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode" {
432+
code = d.ErrorCode
433+
break
434+
}
435+
}
436+
if code == "" {
437+
code = fe.Error.Status
438+
}
439+
msg := fcmErrorCodes[code]
426440
if msg == "" {
427441
msg = fmt.Sprintf("server responded with an unknown error; response: %s", string(resp.Body))
428442
}

messaging/messaging_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ func TestSendError(t *testing.T) {
633633
resp: "{\"error\": {\"status\": \"NOT_FOUND\", \"message\": \"test error\"}}",
634634
want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered",
635635
},
636+
{
637+
resp: `{"error": {"status": "INVALID_ARGUMENT", "message": "test error", "details": [` +
638+
`{"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode", "errorCode": "UNREGISTERED"}]}}`,
639+
want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered",
640+
},
636641
{
637642
resp: "not json",
638643
want: "http error status: 500; reason: server responded with an unknown error; response: not json",

0 commit comments

Comments
 (0)