Skip to content

Improvement: Add Default:TooManyRequests error code #670

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-670.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add Default:TooManyRequests error code
links:
- https://github.com/palantir/conjure-go-runtime/pull/670
10 changes: 10 additions & 0 deletions conjure-go-contract/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ func IsRequestEntityTooLarge(err error) bool {
return isErrorOfType(err, DefaultRequestEntityTooLarge)
}

// WrapWithTooManyRequests returns new error instance of default too many requests type wrapping an existing error.
func WrapWithTooManyRequests(cause error, parameters ...wparams.ParamStorer) Error {
return newGenericError(cause, DefaultTooManyRequests, wparams.NewParamStorer(parameters...))
}

// IsTooManyRequests returns true if an error is an instance of default too many requests.
func IsTooManyRequests(err error) bool {
return isErrorOfType(err, DefaultTooManyRequests)
}

// NewFailedPrecondition returns new error instance of default failed precondition type.
func NewFailedPrecondition(parameters ...wparams.ParamStorer) Error {
return newGenericError(nil, DefaultFailedPrecondition, wparams.NewParamStorer(parameters...))
Expand Down
8 changes: 8 additions & 0 deletions conjure-go-contract/errors/error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
Conflict
// RequestEntityTooLarge has status code 413 RequestEntityTooLarge.
RequestEntityTooLarge
// TooManyRequests has status code 429 TooManyRequests.
TooManyRequests
// FailedPrecondition has status code 500 InternalServerError.
FailedPrecondition
// Internal has status code 500 InternalServerError.
Expand Down Expand Up @@ -77,6 +79,8 @@ func (ec ErrorCode) StatusCode() int {
return http.StatusConflict
case RequestEntityTooLarge:
return http.StatusRequestEntityTooLarge
case TooManyRequests:
return http.StatusTooManyRequests
case FailedPrecondition:
return http.StatusInternalServerError
case Internal:
Expand Down Expand Up @@ -108,6 +112,8 @@ func (ec ErrorCode) String() string {
return "CONFLICT"
case RequestEntityTooLarge:
return "REQUEST_ENTITY_TOO_LARGE"
case TooManyRequests:
return "TOO_MANY_REQUESTS"
case FailedPrecondition:
return "FAILED_PRECONDITION"
case Internal:
Expand Down Expand Up @@ -142,6 +148,8 @@ func (ec *ErrorCode) UnmarshalText(data []byte) error {
*ec = Conflict
case "REQUEST_ENTITY_TOO_LARGE":
*ec = RequestEntityTooLarge
case "TOO_MANY_REQUESTS":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to get this added to the spec before adding to this library: https://github.com/palantir/conjure/blob/master/docs/spec/conjure_definitions.md#errorcode

*ec = TooManyRequests
case "FAILED_PRECONDITION":
*ec = FailedPrecondition
case "INTERNAL":
Expand Down
2 changes: 2 additions & 0 deletions conjure-go-contract/errors/error_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestErrorCode_String(t *testing.T) {
errors.NotFound: "NOT_FOUND",
errors.Conflict: "CONFLICT",
errors.RequestEntityTooLarge: "REQUEST_ENTITY_TOO_LARGE",
errors.TooManyRequests: "TOO_MANY_REQUESTS",
errors.FailedPrecondition: "FAILED_PRECONDITION",
errors.Internal: "INTERNAL",
errors.Timeout: "TIMEOUT",
Expand All @@ -53,6 +54,7 @@ var validErrorCodes = []errors.ErrorCode{
errors.NotFound,
errors.Conflict,
errors.RequestEntityTooLarge,
errors.TooManyRequests,
errors.FailedPrecondition,
errors.Internal,
errors.Timeout,
Expand Down
4 changes: 4 additions & 0 deletions conjure-go-contract/errors/error_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
DefaultNotFound = ErrorType{NotFound, errorNameNotFound}
DefaultConflict = ErrorType{Conflict, errorNameConflict}
DefaultRequestEntityTooLarge = ErrorType{RequestEntityTooLarge, errorNameRequestEntityTooLarge}
DefaultTooManyRequests = ErrorType{TooManyRequests, errorNameTooManyRequests}
DefaultFailedPrecondition = ErrorType{FailedPrecondition, errorNameFailedPrecondition}
DefaultInternal = ErrorType{Internal, errorNameInternal}
DefaultTimeout = ErrorType{Timeout, errorNameTimeout}
Expand Down Expand Up @@ -117,6 +118,7 @@ const (
errorNameNotFound = "Default:NotFound"
errorNameConflict = "Default:Conflict"
errorNameRequestEntityTooLarge = "Default:RequestEntityTooLarge"
errorNameTooManyRequests = "Default:TooManyRequests"
errorNameFailedPrecondition = "Default:FailedPrecondition"
errorNameInternal = "Default:Internal"
errorNameTimeout = "Default:Timeout"
Expand All @@ -134,6 +136,7 @@ func verifyErrorNameString(name string) error {
case errorNameNotFound:
case errorNameConflict:
case errorNameRequestEntityTooLarge:
case errorNameTooManyRequests:
case errorNameFailedPrecondition:
case errorNameInternal:
case errorNameTimeout:
Expand All @@ -154,6 +157,7 @@ func verifyErrorCodeErrorNameCombination(code ErrorCode, name string) error {
case DefaultNotFound:
case DefaultConflict:
case DefaultRequestEntityTooLarge:
case DefaultTooManyRequests:
case DefaultFailedPrecondition:
case DefaultInternal:
case DefaultTimeout:
Expand Down