Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for TooManyRequests (429) error code #258

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clientcompat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func testNoop(cc *clientCompat, s *httptest.Server, clientBin string) {
twirp.NotFound, twirp.BadRoute, twirp.AlreadyExists, twirp.PermissionDenied,
twirp.Unauthenticated, twirp.ResourceExhausted, twirp.FailedPrecondition,
twirp.Aborted, twirp.OutOfRange, twirp.Unimplemented, twirp.Internal,
twirp.Unavailable, twirp.DataLoss,
twirp.Unavailable, twirp.DataLoss, twirp.TooManyRequests,
} {
testcase(
fmt.Sprintf("%q error parsing", code),
Expand Down
1 change: 1 addition & 0 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Each error code is defined by a constant in the `twirp` package:
| Internal | internal | 500 Internal Server Error
| Unavailable | unavailable | 503 Service Unavailable
| DataLoss | dataloss | 500 Internal Server Error
| TooManyRequests | too_many_requests | 429 Too Many Requests

For more information on each code, see the [Errors Spec](spec_v5.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/spec_v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,5 @@ corresponding HTTP Status Code for the response.
| internal | 500 | When some invariants expected by the underlying system have been broken. In other words, something bad happened in the library or backend service. Twirp specific issues like wire and serialization problems are also reported as "internal" errors.
| unavailable | 503 | The service is currently unavailable. This is most likely a transient condition and may be corrected by retrying with a backoff.
| dataloss | 500 | The operation resulted in unrecoverable data loss or corruption.

| too_many_requests | 429 | The client is making too many requests. They should slow down their rate of requests.

5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ const (
// DataLoss indicates unrecoverable data loss or corruption.
DataLoss ErrorCode = "data_loss"

// TooManyRequests indicates that the client is making too many requests.
TooManyRequests ErrorCode = "too_many_requests"

// NoError is the zero-value, is considered an empty error and should not be
// used.
NoError ErrorCode = ""
Expand Down Expand Up @@ -279,6 +282,8 @@ func ServerHTTPStatusFromErrorCode(code ErrorCode) int {
return 503 // Service Unavailable
case DataLoss:
return 500 // Internal Server Error
case TooManyRequests:
return 429 // Too Many Requests
case NoError:
return 200 // OK
default:
Expand Down