From cf0c0e964e672eb23e97e7beed3ad8e29e40ec70 Mon Sep 17 00:00:00 2001 From: David Xu <62825020+dxaviud@users.noreply.github.com> Date: Mon, 26 Dec 2022 00:35:24 -0500 Subject: [PATCH] Fix index out of bounds panic for Retry-After header (#122) * fix index out of bounds * RateLimitedError --- client.go | 9 +++++++-- error.go | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 68e9580..69faaac 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/error.go b/error.go index 15fdf53..4813a23 100644 --- a/error.go +++ b/error.go @@ -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 +}