From a431c4ff5b563bac58db2eb27534c3f16e8e956a Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 12 Jul 2021 14:36:08 -0400 Subject: [PATCH] better error handling --- cmd/apollo-worker-notifications/main.go | 39 ++++++++++++++++++++++--- internal/data/accounts.go | 1 + internal/reddit/client.go | 19 +++++++++++- internal/reddit/types.go | 9 ++++++ 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/cmd/apollo-worker-notifications/main.go b/cmd/apollo-worker-notifications/main.go index 54f01ae..f987881 100644 --- a/cmd/apollo-worker-notifications/main.go +++ b/cmd/apollo-worker-notifications/main.go @@ -222,6 +222,7 @@ func (c *Consumer) Consume(delivery rmq.Delivery) { stmt := `SELECT id, username, + account_id, access_token, refresh_token, expires_at, @@ -233,6 +234,7 @@ func (c *Consumer) Consume(delivery rmq.Delivery) { if err := c.pool.QueryRow(ctx, stmt, id).Scan( &account.ID, &account.Username, + &account.AccountID, &account.AccessToken, &account.RefreshToken, &account.ExpiresAt, @@ -418,7 +420,19 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount postTitle = fmt.Sprintf("%s…", postTitle[0:75]) } - payload := payload.NewPayload().Sound("traloop.wav").AlertBody(postBody).Custom("author", msg.Author).Custom("parent_id", msg.ParentID).AlertSummaryArg(msg.Author).MutableContent().Badge(badgeCount).Custom("post_title", msg.LinkTitle).Custom("destination_author", msg.Destination).Custom("subreddit", msg.Subreddit) + payload := payload. + NewPayload(). + AlertBody(postBody). + AlertSummaryArg(msg.Author). + Badge(badgeCount). + Custom("account_id", acct.AccountID). + Custom("author", msg.Author). + Custom("destination_author", msg.Destination). + Custom("parent_id", msg.ParentID). + Custom("post_title", msg.LinkTitle). + Custom("subreddit", msg.Subreddit). + MutableContent(). + Sound("traloop.wav") switch { case (msg.Kind == "t1" && msg.Type == "username_mention"): @@ -436,16 +450,33 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount break case (msg.Kind == "t1" && msg.Type == "post_reply"): title := fmt.Sprintf(`%s to “%s”`, msg.Author, postTitle) - payload = payload.AlertTitle(title).Custom("type", "post").Category("inbox-post-reply").Custom("subject", "comment").ThreadID("comment").Custom("post_id", msg.ID) + payload = payload. + AlertTitle(title). + Category("inbox-post-reply"). + Custom("post_id", msg.ID). + Custom("subject", "comment"). + Custom("type", "post"). + ThreadID("comment") break case (msg.Kind == "t1" && msg.Type == "comment_reply"): title := fmt.Sprintf(`%s in “%s”`, msg.Author, postTitle) _, postID := reddit.SplitID(msg.ParentID) - payload = payload.AlertTitle(title).Custom("type", "comment").Category("inbox-comment-reply").Custom("subject", "comment").ThreadID("comment").Custom("post_id", postID).Custom("comment_id", msg.ID) + payload = payload. + AlertTitle(title). + Category("inbox-comment-reply"). + Custom("comment_id", msg.ID). + Custom("post_id", postID). + Custom("subject", "comment"). + Custom("type", "comment"). + ThreadID("comment") break case (msg.Kind == "t4"): title := fmt.Sprintf(`Message from %s`, msg.Author) - payload = payload.AlertTitle(title).AlertSubtitle(postTitle).Custom("type", "private-message").Category("inbox-private-message") + payload = payload. + AlertTitle(title). + AlertSubtitle(postTitle). + Category("inbox-private-message"). + Custom("type", "private-message") break } diff --git a/internal/data/accounts.go b/internal/data/accounts.go index f110254..ea7ba04 100644 --- a/internal/data/accounts.go +++ b/internal/data/accounts.go @@ -8,6 +8,7 @@ import ( type Account struct { ID int64 Username string + AccountID string AccessToken string RefreshToken string ExpiresAt int64 diff --git a/internal/reddit/client.go b/internal/reddit/client.go index 8ed2c07..02cc3b4 100644 --- a/internal/reddit/client.go +++ b/internal/reddit/client.go @@ -2,6 +2,7 @@ package reddit import ( "encoding/json" + "fmt" "io/ioutil" "net/http" "net/http/httptrace" @@ -93,7 +94,23 @@ func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) { } defer resp.Body.Close() - return ioutil.ReadAll(resp.Body) + bb, err := ioutil.ReadAll(resp.Body) + if err != nil { + rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) + return nil, err + } + + if resp.StatusCode != 200 { + rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) + + // Try to parse a json error. Otherwise we generate a generic one + rerr := &Error{} + if jerr := json.Unmarshal(bb, rerr); jerr != nil { + return nil, fmt.Errorf("error from reddit: %d", resp.StatusCode) + } + return nil, rerr + } + return bb, nil } func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) { diff --git a/internal/reddit/types.go b/internal/reddit/types.go index eba8247..d87eaa2 100644 --- a/internal/reddit/types.go +++ b/internal/reddit/types.go @@ -2,6 +2,15 @@ package reddit import "fmt" +type Error struct { + Message string `json:"message"` + Code int `json:"error"` +} + +func (err *Error) Error() string { + return fmt.Sprintf("%s (%d)", err.Message, err.Code) +} + type Message struct { ID string `json:"id"` Kind string `json:"kind"`