Skip to content

Commit

Permalink
Add a public slice of generic error codes
Browse files Browse the repository at this point in the history
This is useful if you want to validate that error code prefixes
matche one of these well-known generic error codes, for exmaple.
  • Loading branch information
Will Sewell committed Nov 24, 2023
1 parent 4a94093 commit 99fb5bd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 99fb5bd

Please sign in to comment.