generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ErrTooManyRequests to support Retry-After and 429
- Loading branch information
Showing
4 changed files
with
205 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
ipld "github.com/ipfs/go-ipld-format" | ||
"github.com/ipfs/go-path/resolver" | ||
) | ||
|
||
var ( | ||
ErrGatewayTimeout = errors.New(http.StatusText(http.StatusGatewayTimeout)) | ||
ErrBadGateway = errors.New(http.StatusText(http.StatusBadGateway)) | ||
) | ||
|
||
type ErrTooManyRequests struct { | ||
RetryAfter uint64 | ||
} | ||
|
||
func (e *ErrTooManyRequests) Error() string { | ||
return http.StatusText(http.StatusTooManyRequests) | ||
} | ||
|
||
func (e *ErrTooManyRequests) Is(err error) bool { | ||
switch err.(type) { | ||
case *ErrTooManyRequests: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func webError(w http.ResponseWriter, err error, defaultCode int) { | ||
code := defaultCode | ||
|
||
switch { | ||
case isErrNotFound(err): | ||
code = http.StatusNotFound | ||
case errors.Is(err, ErrGatewayTimeout), | ||
errors.Is(err, context.DeadlineExceeded): | ||
code = http.StatusGatewayTimeout | ||
case errors.Is(err, ErrBadGateway): | ||
code = http.StatusBadGateway | ||
case errors.Is(err, &ErrTooManyRequests{}): | ||
var tooManyRequests *ErrTooManyRequests | ||
_ = errors.As(err, &tooManyRequests) | ||
if tooManyRequests.RetryAfter > 0 { | ||
w.Header().Set("Retry-After", strconv.FormatUint(tooManyRequests.RetryAfter, 10)) | ||
} | ||
|
||
code = http.StatusTooManyRequests | ||
} | ||
|
||
http.Error(w, err.Error(), code) | ||
if code >= 500 { | ||
log.Warnf("server error: %s", err) | ||
} | ||
} | ||
|
||
func isErrNotFound(err error) bool { | ||
if ipld.IsNotFound(err) { | ||
return true | ||
} | ||
|
||
// Checks if err is a resolver.ErrNoLink. resolver.ErrNoLink does not implement | ||
// the .Is interface and cannot be directly compared to. Therefore, errors.Is | ||
// always returns false with it. | ||
for { | ||
_, ok := err.(resolver.ErrNoLink) | ||
if ok { | ||
return true | ||
} | ||
|
||
err = errors.Unwrap(err) | ||
if err == nil { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
func webRequestError(w http.ResponseWriter, err *requestError) { | ||
webError(w, err.Err, err.StatusCode) | ||
} | ||
|
||
// Custom type for collecting error details to be handled by `webRequestError` | ||
type requestError struct { | ||
StatusCode int | ||
Err error | ||
} | ||
|
||
func newRequestError(err error, statusCode int) *requestError { | ||
return &requestError{ | ||
Err: err, | ||
StatusCode: statusCode, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package gateway | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrTooManyRequestsIs(t *testing.T) { | ||
var err error | ||
|
||
err = &ErrTooManyRequests{RetryAfter: 10} | ||
assert.True(t, errors.Is(err, &ErrTooManyRequests{}), "pointer to error must be error") | ||
|
||
err = fmt.Errorf("wrapped: %w", err) | ||
assert.True(t, errors.Is(err, &ErrTooManyRequests{}), "wrapped pointer to error must be error") | ||
} | ||
|
||
func TestErrTooManyRequestsAs(t *testing.T) { | ||
var ( | ||
err error | ||
errTMR *ErrTooManyRequests | ||
) | ||
|
||
err = &ErrTooManyRequests{RetryAfter: 25} | ||
assert.True(t, errors.As(err, &errTMR), "pointer to error must be error") | ||
assert.EqualValues(t, errTMR.RetryAfter, 25) | ||
|
||
err = fmt.Errorf("wrapped: %w", err) | ||
assert.True(t, errors.As(err, &errTMR), "wrapped pointer to error must be error") | ||
assert.EqualValues(t, errTMR.RetryAfter, 25) | ||
} | ||
|
||
func TestWebError(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("429 Too Many Requests", func(t *testing.T) { | ||
err := fmt.Errorf("wrapped for testing: %w", &ErrTooManyRequests{}) | ||
w := httptest.NewRecorder() | ||
webError(w, err, http.StatusInternalServerError) | ||
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode) | ||
assert.Zero(t, len(w.Result().Header.Values("Retry-After"))) | ||
}) | ||
|
||
t.Run("429 Too Many Requests with Retry-After header", func(t *testing.T) { | ||
err := &ErrTooManyRequests{RetryAfter: 25} | ||
w := httptest.NewRecorder() | ||
webError(w, err, http.StatusInternalServerError) | ||
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode) | ||
assert.Equal(t, "25", w.Result().Header.Get("Retry-After")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters