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 +}