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
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
}

View file

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

View file

@ -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) {

View file

@ -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"`