Expose rate limit errors in public API

Signed-off-by: Vitaliy Potyarkin <v.potyarkin@yadro.com>
This commit is contained in:
Vitaliy Potyarkin 2024-11-05 13:32:07 +03:00
parent 10ccc57587
commit 256957db5c
2 changed files with 20 additions and 0 deletions

View file

@ -119,6 +119,14 @@ func (d *Doer) formatUserAgent() string {
func checkError(req *http.Request, resp *http.Response) error { func checkError(req *http.Request, resp *http.Response) error {
if resp.StatusCode >= http.StatusBadRequest { if resp.StatusCode >= http.StatusBadRequest {
if resp.StatusCode == http.StatusTooManyRequests {
return acme.TooManyRequestsError{
StatusCode: resp.StatusCode,
Method: req.Method,
URL: req.URL,
}
}
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("%d :: %s :: %s :: %w", resp.StatusCode, req.Method, req.URL, err) return fmt.Errorf("%d :: %s :: %s :: %w", resp.StatusCode, req.Method, req.URL, err)

View file

@ -2,6 +2,7 @@ package acme
import ( import (
"fmt" "fmt"
"net/url"
) )
// Errors types. // Errors types.
@ -56,3 +57,14 @@ func (p ProblemDetails) Error() string {
type NonceError struct { type NonceError struct {
*ProblemDetails *ProblemDetails
} }
// TooManyRequestsError represents API rate limit violations reported by server.
type TooManyRequestsError struct {
StatusCode int
Method string
URL *url.URL
}
func (e TooManyRequestsError) Error() string {
return fmt.Sprintf("too many requests: HTTP %d: %s (%s)", e.StatusCode, e.URL, e.Method)
}