be more forgiving with reddit errors

This commit is contained in:
Andre Medeiros 2022-10-26 18:28:11 -04:00
parent 840d644a4e
commit 2b03f4f884
5 changed files with 66 additions and 17 deletions

View file

@ -26,6 +26,7 @@ func TestValidate(t *testing.T) {
}
for scenario, tc := range tt {
tc := tc
t.Run(scenario, func(t *testing.T) {
t.Parallel()

View file

@ -284,6 +284,23 @@ func (rc *Client) SubredditAbout(ctx context.Context, subreddit string, opts ...
return sr, nil
}
func obfuscate(tok string) string {
tl := len(tok)
if tl < 6 {
return "<SHORT>"
}
return fmt.Sprintf("%s...%s", tok[0:3], tok[tl-3:tl])
}
func (rac *AuthenticatedClient) ObfuscatedAccessToken() string {
return obfuscate(rac.accessToken)
}
func (rac *AuthenticatedClient) ObfuscatedRefreshToken() string {
return obfuscate(rac.refreshToken)
}
func (rac *AuthenticatedClient) request(ctx context.Context, r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) {
if rac.isRateLimited() {
return nil, ErrRateLimited

View file

@ -0,0 +1,33 @@
package reddit_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/christianselig/apollo-backend/internal/reddit"
)
func TestAuthenticatedClientObfuscatedToken(t *testing.T) {
t.Parallel()
rc := reddit.NewClient("<SECRET>", "<SECRET>", nil, nil, 1)
type test struct {
have string
want string
}
tests := []test{
{"abc", "<SHORT>"},
{"abcdefghi", "abc...ghi"},
}
for _, tc := range tests {
tc := tc
rac := rc.NewAuthenticatedClient("<ID>", "<REFRESH>", tc.have)
got := rac.ObfuscatedAccessToken()
assert.Equal(t, tc.want, got)
}
}

View file

@ -5,9 +5,10 @@ import (
"testing"
"time"
"github.com/christianselig/apollo-backend/internal/reddit"
"github.com/stretchr/testify/assert"
"github.com/valyala/fastjson"
"github.com/christianselig/apollo-backend/internal/reddit"
)
var pool = &fastjson.ParserPool{}

View file

@ -173,26 +173,20 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) {
if la.TokenExpiresAt.Before(now.Add(5 * time.Minute)) {
lac.logger.Debug("refreshing reddit token",
zap.String("live_activity#apns_token", at),
zap.String("reddit#id", la.RedditAccountID),
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
)
tokens, err := rac.RefreshTokens(lac)
if err != nil {
if err != reddit.ErrOauthRevoked {
lac.logger.Error("failed to refresh reddit tokens",
zap.Error(err),
zap.String("live_activity#apns_token", at),
)
return
}
err = lac.liveActivityRepo.Delete(lac, at)
if err != nil {
lac.logger.Error("failed to remove revoked account",
zap.Error(err),
zap.String("live_activity#apns_token", at),
)
}
lac.logger.Error("failed to refresh reddit tokens",
zap.Error(err),
zap.String("live_activity#apns_token", at),
zap.String("reddit#id", la.RedditAccountID),
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
)
return
}
@ -213,6 +207,9 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) {
lac.logger.Error("failed to fetch latest comments",
zap.Error(err),
zap.String("live_activity#apns_token", at),
zap.String("reddit#id", la.RedditAccountID),
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
)
return
}