Skip to content

Commit cf0c0e9

Browse files
authored
Fix index out of bounds panic for Retry-After header (jomei#122)
* fix index out of bounds * RateLimitedError
1 parent 4824a20 commit cf0c0e9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,15 @@ func (c *Client) request(ctx context.Context, method string, urlStr string, quer
142142

143143
failedAttempts++
144144
if failedAttempts == c.maxRetries {
145-
return nil, fmt.Errorf("Retry request with 429 response failed after %d retries", failedAttempts)
145+
return nil, &RateLimitedError{Message: fmt.Sprintf("Retry request with 429 response failed after %d retries", failedAttempts)}
146146
}
147147
// https://developers.notion.com/reference/request-limits#rate-limits
148-
retryAfter := res.Header["Retry-After"][0]
148+
retryAfterHeader := res.Header["Retry-After"]
149+
if len(retryAfterHeader) == 0 {
150+
return nil, &RateLimitedError{Message: "Retry-After header missing from Notion API response headers for 429 response"}
151+
}
152+
retryAfter := retryAfterHeader[0]
153+
149154
waitSeconds, err := strconv.Atoi(retryAfter)
150155
if err != nil {
151156
break // should not happen

error.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ type Error struct {
1212
func (e *Error) Error() string {
1313
return e.Message
1414
}
15+
16+
type RateLimitedError struct {
17+
Message string
18+
}
19+
20+
func (e *RateLimitedError) Error() string {
21+
return e.Message
22+
}

0 commit comments

Comments
 (0)