Expose rate limit errors in public API
Signed-off-by: Vitaliy Potyarkin <v.potyarkin@yadro.com>
This commit is contained in:
parent
10ccc57587
commit
256957db5c
2 changed files with 20 additions and 0 deletions
|
@ -119,6 +119,14 @@ func (d *Doer) formatUserAgent() string {
|
|||
|
||||
func checkError(req *http.Request, resp *http.Response) error {
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%d :: %s :: %s :: %w", resp.StatusCode, req.Method, req.URL, err)
|
||||
|
|
|
@ -2,6 +2,7 @@ package acme
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Errors types.
|
||||
|
@ -56,3 +57,14 @@ func (p ProblemDetails) Error() string {
|
|||
type NonceError struct {
|
||||
*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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue