Skip to content

Commit

Permalink
Fix index out of bounds panic for Retry-After header (#122)
Browse files Browse the repository at this point in the history
* fix index out of bounds

* RateLimitedError
  • Loading branch information
dxaviud authored Dec 26, 2022
1 parent 4824a20 commit cf0c0e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ func (c *Client) request(ctx context.Context, method string, urlStr string, quer

failedAttempts++
if failedAttempts == c.maxRetries {
return nil, fmt.Errorf("Retry request with 429 response failed after %d retries", failedAttempts)
return nil, &RateLimitedError{Message: fmt.Sprintf("Retry request with 429 response failed after %d retries", failedAttempts)}
}
// https://developers.notion.com/reference/request-limits#rate-limits
retryAfter := res.Header["Retry-After"][0]
retryAfterHeader := res.Header["Retry-After"]
if len(retryAfterHeader) == 0 {
return nil, &RateLimitedError{Message: "Retry-After header missing from Notion API response headers for 429 response"}
}
retryAfter := retryAfterHeader[0]

waitSeconds, err := strconv.Atoi(retryAfter)
if err != nil {
break // should not happen
Expand Down
8 changes: 8 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ type Error struct {
func (e *Error) Error() string {
return e.Message
}

type RateLimitedError struct {
Message string
}

func (e *RateLimitedError) Error() string {
return e.Message
}

0 comments on commit cf0c0e9

Please sign in to comment.