Skip to content

Commit cda0683

Browse files
committed
fix: match custom errors correctly
1 parent 6d68321 commit cda0683

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

internal/twitter/twitter.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"time"
1818
)
1919

20-
const rateLimitError = "429 Too Many Requests"
20+
const TooManyRequestsErrorMessage = "429 Too Many Requests"
2121

2222
func updateTwitterCookies(config types.TradogeConfig) {
2323
scraper := twitterscraper.New()
@@ -114,11 +114,11 @@ func searchTweets(scraper *twitterscraper.Scraper, query string, config types.Tr
114114
log.Println("Total tweets:", counter)
115115
}
116116

117-
type TweeterConnectionError struct {
117+
type ConnectionError struct {
118118
Err error
119119
}
120120

121-
func (t *TweeterConnectionError) Error() string {
121+
func (t *ConnectionError) Error() string {
122122
return fmt.Sprintf("Failed to connect to Twitter: %v", t.Err)
123123
}
124124

@@ -140,10 +140,10 @@ func getLastMatchingTweet(scraper *twitterscraper.Scraper, query string) (*twitt
140140
return &tweet.Tweet, nil
141141
}
142142
log.Printf("Failed to get last tweet: %v", tweet.Error)
143-
if strings.Contains(tweet.Error.Error(), rateLimitError) {
143+
if strings.Contains(tweet.Error.Error(), TooManyRequestsErrorMessage) {
144144
return nil, &RateLimitError{Err: tweet.Error}
145145
}
146-
return nil, &TweeterConnectionError{Err: tweet.Error}
146+
return nil, &ConnectionError{Err: tweet.Error}
147147
}
148148
return nil, TweetNotFoundError
149149
}
@@ -177,18 +177,19 @@ func MonitorTweets(config types.TradogeConfig) {
177177
if err != nil {
178178
log.Println("Failed to get last tweet:", err)
179179
heartbeat.SendFailure()
180-
}
181180

182-
if errors.Is(err, &RateLimitError{}) {
183-
log.Println("Rate limited, waiting 5 minutes...")
184-
time.Sleep(5 * time.Minute)
185-
continue
186-
} else if errors.Is(err, &TweeterConnectionError{}) {
187-
log.Println("Failed to connect to Twitter, waiting 5 minutes...")
188-
time.Sleep(5 * time.Minute)
189-
continue
190-
} else if errors.Is(err, TweetNotFoundError) {
191-
// No new tweet found so we continue and check again after the delay
181+
var rateLimitError *RateLimitError
182+
var connectionError *ConnectionError
183+
184+
if errors.As(err, &rateLimitError) {
185+
log.Println("Rate limited, waiting 5 minutes...")
186+
time.Sleep(5 * time.Minute)
187+
} else if errors.As(err, &connectionError) {
188+
log.Println("Failed to connect to Twitter, waiting 5 minutes...")
189+
time.Sleep(5 * time.Minute)
190+
}
191+
// for TweetNotFoundError No new tweet found so we continue and check again after the delay
192+
// for other errors we just log and continue
192193
continue
193194
}
194195

0 commit comments

Comments
 (0)