From 38ed6ed863f78ceaed0ca0550315d37e26411da9 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 29 Mar 2018 10:33:43 -0700 Subject: [PATCH] Reading FCM error code from details section --- messaging/messaging.go | 18 ++++++++++++++++-- messaging/messaging_test.go | 5 +++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/messaging/messaging.go b/messaging/messaging.go index fb6fa5dd..b4c31c7b 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -381,7 +381,11 @@ type fcmResponse struct { type fcmError struct { Error struct { - Status string `json:"status"` + Status string `json:"status"` + Details []struct { + Type string `json:"@type"` + ErrorCode string `json:"errorCode"` + } } `json:"error"` } @@ -422,7 +426,17 @@ func (c *Client) makeSendRequest(ctx context.Context, req *fcmRequest) (string, var fe fcmError json.Unmarshal(resp.Body, &fe) // ignore any json parse errors at this level - msg := fcmErrorCodes[fe.Error.Status] + var code string + for _, d := range fe.Error.Details { + if d.Type == "type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode" { + code = d.ErrorCode + break + } + } + if code == "" { + code = fe.Error.Status + } + msg := fcmErrorCodes[code] if msg == "" { msg = fmt.Sprintf("server responded with an unknown error; response: %s", string(resp.Body)) } diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index 1d6d3ad5..8a39ffc6 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -633,6 +633,11 @@ func TestSendError(t *testing.T) { 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: `{"error": {"status": "INVALID_ARGUMENT", "message": "test error", "details": [` + + `{"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode", "errorCode": "UNREGISTERED"}]}}`, + 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",