diff --git a/changelog/@unreleased/pr-670.v2.yml b/changelog/@unreleased/pr-670.v2.yml new file mode 100644 index 00000000..73f991b7 --- /dev/null +++ b/changelog/@unreleased/pr-670.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Add Default:TooManyRequests error code + links: + - https://github.com/palantir/conjure-go-runtime/pull/670 diff --git a/conjure-go-contract/errors/error.go b/conjure-go-contract/errors/error.go index a6cc89c9..b600322c 100644 --- a/conjure-go-contract/errors/error.go +++ b/conjure-go-contract/errors/error.go @@ -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...)) diff --git a/conjure-go-contract/errors/error_code.go b/conjure-go-contract/errors/error_code.go index ad614d3c..78bd3580 100644 --- a/conjure-go-contract/errors/error_code.go +++ b/conjure-go-contract/errors/error_code.go @@ -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. @@ -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: @@ -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: @@ -142,6 +148,8 @@ func (ec *ErrorCode) UnmarshalText(data []byte) error { *ec = Conflict case "REQUEST_ENTITY_TOO_LARGE": *ec = RequestEntityTooLarge + case "TOO_MANY_REQUESTS": + *ec = TooManyRequests case "FAILED_PRECONDITION": *ec = FailedPrecondition case "INTERNAL": diff --git a/conjure-go-contract/errors/error_code_test.go b/conjure-go-contract/errors/error_code_test.go index 8f904493..c57ecce0 100644 --- a/conjure-go-contract/errors/error_code_test.go +++ b/conjure-go-contract/errors/error_code_test.go @@ -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", @@ -53,6 +54,7 @@ var validErrorCodes = []errors.ErrorCode{ errors.NotFound, errors.Conflict, errors.RequestEntityTooLarge, + errors.TooManyRequests, errors.FailedPrecondition, errors.Internal, errors.Timeout, diff --git a/conjure-go-contract/errors/error_type.go b/conjure-go-contract/errors/error_type.go index 2e9d7a04..eaf592a1 100644 --- a/conjure-go-contract/errors/error_type.go +++ b/conjure-go-contract/errors/error_type.go @@ -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} @@ -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" @@ -134,6 +136,7 @@ func verifyErrorNameString(name string) error { case errorNameNotFound: case errorNameConflict: case errorNameRequestEntityTooLarge: + case errorNameTooManyRequests: case errorNameFailedPrecondition: case errorNameInternal: case errorNameTimeout: @@ -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: