From 99fb5bdf27b40b44858ee09add2bf190899a8eb4 Mon Sep 17 00:00:00 2001 From: Will Sewell Date: Fri, 24 Nov 2023 11:15:01 +0000 Subject: [PATCH] Add a public slice of generic error codes This is useful if you want to validate that error code prefixes matche one of these well-known generic error codes, for exmaple. --- errors.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index 1636bfe..dffc743 100644 --- a/errors.go +++ b/errors.go @@ -29,6 +29,7 @@ import ( // Generic error codes. Each of these has their own constructor for convenience. // You can use any string as a code, just use the `New` method. +// Warning: any new generic error code must be added to GenericErrorCodes. const ( ErrBadRequest = "bad_request" ErrBadResponse = "bad_response" @@ -42,6 +43,20 @@ const ( ErrRateLimited = "rate_limited" ) +// GenericErrorCodes is a list of all well known generic error codes. +var GenericErrorCodes = []string{ + ErrBadRequest, + ErrBadResponse, + ErrForbidden, + ErrInternalService, + ErrNotFound, + ErrPreconditionFailed, + ErrTimeout, + ErrUnauthorized, + ErrUnknown, + ErrRateLimited, +} + var retryableCodes = []string{ ErrInternalService, ErrTimeout, @@ -188,9 +203,9 @@ func (p *Error) SetIsRetryable(value bool) { } } -// SetIsUnexpected can be used to explicitly mark an error as unexpected or not. In practice the vast majority of +// SetIsUnexpected can be used to explicitly mark an error as unexpected or not. In practice the vast majority of // code should not need to use this. An example use case might be when returning a validation error that must -// mean there is a coding mistake somewhere (e.g. default statement in a switch that is never expected to be +// mean there is a coding mistake somewhere (e.g. default statement in a switch that is never expected to be // taken). By marking the error as unexpected there is a greater chance that an alert will be sent. func (p *Error) SetIsUnexpected(value bool) { if value {