Skip to content

Commit 6cf2210

Browse files
authored
Adding additional details to the error message when available (#129)
1 parent 201a4f3 commit 6cf2210

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

messaging/messaging.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ type fcmResponse struct {
382382
type fcmError struct {
383383
Error struct {
384384
Status string `json:"status"`
385+
Message string `json:"message"`
385386
Details []struct {
386387
Type string `json:"@type"`
387388
ErrorCode string `json:"errorCode"`
@@ -439,6 +440,8 @@ func (c *Client) makeSendRequest(ctx context.Context, req *fcmRequest) (string,
439440
msg := fcmErrorCodes[code]
440441
if msg == "" {
441442
msg = fmt.Sprintf("server responded with an unknown error; response: %s", string(resp.Body))
443+
} else if fe.Error.Message != "" {
444+
msg += "; details: " + fe.Error.Message
442445
}
443446
return "", fmt.Errorf("http error status: %d; reason: %s", resp.Status, msg)
444447
}

messaging/messaging_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,18 @@ func TestSendError(t *testing.T) {
627627
},
628628
{
629629
resp: "{\"error\": {\"status\": \"INVALID_ARGUMENT\", \"message\": \"test error\"}}",
630-
want: "http error status: 500; reason: request contains an invalid argument; code: invalid-argument",
630+
want: "http error status: 500; reason: request contains an invalid argument; code: invalid-argument; details: test error",
631631
},
632632
{
633633
resp: "{\"error\": {\"status\": \"NOT_FOUND\", \"message\": \"test error\"}}",
634-
want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered",
634+
want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered; " +
635+
"details: test error",
635636
},
636637
{
637638
resp: `{"error": {"status": "INVALID_ARGUMENT", "message": "test error", "details": [` +
638639
`{"@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+
want: "http error status: 500; reason: app instance has been unregistered; code: registration-token-not-registered; " +
641+
"details: test error",
640642
},
641643
{
642644
resp: "not json",

0 commit comments

Comments
 (0)