better error handling

This commit is contained in:
Andre Medeiros 2021-07-12 14:36:08 -04:00
parent fad7191035
commit a431c4ff5b
4 changed files with 63 additions and 5 deletions

View file

@ -222,6 +222,7 @@ func (c *Consumer) Consume(delivery rmq.Delivery) {
stmt := `SELECT stmt := `SELECT
id, id,
username, username,
account_id,
access_token, access_token,
refresh_token, refresh_token,
expires_at, expires_at,
@ -233,6 +234,7 @@ func (c *Consumer) Consume(delivery rmq.Delivery) {
if err := c.pool.QueryRow(ctx, stmt, id).Scan( if err := c.pool.QueryRow(ctx, stmt, id).Scan(
&account.ID, &account.ID,
&account.Username, &account.Username,
&account.AccountID,
&account.AccessToken, &account.AccessToken,
&account.RefreshToken, &account.RefreshToken,
&account.ExpiresAt, &account.ExpiresAt,
@ -418,7 +420,19 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount
postTitle = fmt.Sprintf("%s…", postTitle[0:75]) 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 { switch {
case (msg.Kind == "t1" && msg.Type == "username_mention"): case (msg.Kind == "t1" && msg.Type == "username_mention"):
@ -436,16 +450,33 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount
break break
case (msg.Kind == "t1" && msg.Type == "post_reply"): case (msg.Kind == "t1" && msg.Type == "post_reply"):
title := fmt.Sprintf(`%s to “%s”`, msg.Author, postTitle) 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 break
case (msg.Kind == "t1" && msg.Type == "comment_reply"): case (msg.Kind == "t1" && msg.Type == "comment_reply"):
title := fmt.Sprintf(`%s in “%s”`, msg.Author, postTitle) title := fmt.Sprintf(`%s in “%s”`, msg.Author, postTitle)
_, postID := reddit.SplitID(msg.ParentID) _, 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 break
case (msg.Kind == "t4"): case (msg.Kind == "t4"):
title := fmt.Sprintf(`Message from %s`, msg.Author) 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 break
} }

View file

@ -8,6 +8,7 @@ import (
type Account struct { type Account struct {
ID int64 ID int64
Username string Username string
AccountID string
AccessToken string AccessToken string
RefreshToken string RefreshToken string
ExpiresAt int64 ExpiresAt int64

View file

@ -2,6 +2,7 @@ package reddit
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptrace" "net/http/httptrace"
@ -93,7 +94,23 @@ func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) {
} }
defer resp.Body.Close() 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) { func (rac *AuthenticatedClient) RefreshTokens() (*RefreshTokenResponse, error) {

View file

@ -2,6 +2,15 @@ package reddit
import "fmt" 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 { type Message struct {
ID string `json:"id"` ID string `json:"id"`
Kind string `json:"kind"` Kind string `json:"kind"`