From 1f919bfab12ab60aea943b8aedb9e9268da995ed Mon Sep 17 00:00:00 2001 From: Benny Wong Date: Thu, 26 Jun 2014 17:46:32 -0400 Subject: [PATCH 1/2] Add typed errors for known error types * Also add response error names as constants - Responses errors are per message, and the others are per request --- sender.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/sender.go b/sender.go index 3c6292a..b7a61f5 100644 --- a/sender.go +++ b/sender.go @@ -25,6 +25,24 @@ const ( // Declared as a mutable variable for testing purposes. var gcmSendEndpoint = GcmSendEndpoint +// Errors +type JSONParseError struct{ error } +type UnauthorizedError struct{ error } +type UnknownError struct{ error } + +const ( + ResponseErrorMissingRegistration = "MissingRegistration" + ResponseErrorInvalidRegistration = "InvalidRegistration" + ResponseErrorMismatchSenderID = "MismatchSenderId" + ResponseErrorNotRegistered = "NotRegistered" + ResponseErrorMessageTooBig = "MessageTooBig" + ResponseErrorInvalidDataKey = "InvalidDataKey" + ResponseErrorInvalidTTL = "InvalidTtl" + ResponseErrorUnavailable = "Unavailable" + ResponseErrorInternalServerError = "InternalServerError" + ResponseErrorInvalidPackageName = "InvalidPackageName" +) + // Sender abstracts the interaction between the application server and the // GCM server. The developer must obtain an API key from the Google APIs // Console page and pass it to the Sender so that it can perform authorized @@ -76,8 +94,14 @@ func (s *Sender) SendNoRetry(msg *Message) (*Response, error) { } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status) + switch resp.StatusCode { + case http.StatusBadRequest: + return nil, JSONParseError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)} + case http.StatusUnauthorized: + return nil, UnauthorizedError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)} + case http.StatusOK: + default: + return nil, UnknownError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)} } body, err := ioutil.ReadAll(resp.Body) From 1c7cef6e71d63bdcbef8d5a4c0606b6c3016a1fd Mon Sep 17 00:00:00 2001 From: Dmitry Traytel Date: Mon, 20 May 2019 17:37:37 -0400 Subject: [PATCH 2/2] Sender: Updated GCM endpoint to FCM --- sender.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sender.go b/sender.go index b7a61f5..d4f73cc 100644 --- a/sender.go +++ b/sender.go @@ -15,7 +15,8 @@ import ( const ( // GcmSendEndpoint is the endpoint for sending messages to the GCM server. - GcmSendEndpoint = "https://android.googleapis.com/gcm/send" + // DT: Replaced GCM endpoint with FCM. Al other references remain the same + GcmSendEndpoint = "https://fcm.googleapis.com/fcm/send" // Initial delay before first retry, without jitter. backoffInitialDelay = 1000 // Maximum delay before a retry.