Documentation ¶
Overview ¶
Description: Bed request error
Description: Custom error with list of details compatible with JSON API spec
Package orerr implements outreach specific error utilities.
Index ¶
- func CancelWithError(ctx context.Context) (c context.Context, cancel func(err error))
- func ExtractErrorMetadata(err error) map[string]string
- func ExtractErrorStatusCategory(err error) statuscodes.StatusCategory
- func ExtractErrorStatusCode(err error) statuscodes.StatusCode
- func Info(err error, info ...log.Marshaler) error
- func IsErrorStatusCategory(err error, category statuscodes.StatusCategory) bool
- func IsErrorStatusCode(err error, code statuscodes.StatusCode) bool
- func IsOneOf(err error, errs ...error) bool
- func IsRetryable(err error) bool
- func Meta(err error, meta map[string]string) error
- func New(err error, opts ...ErrOption) error
- func NewBadRequestError(err error, violations ...Violation) error
- func NewErrDetails(err error, details ...ErrDetail) error
- func NewErrorStatus(errToWrap error, errCode statuscodes.StatusCode) error
- func Retryable(err error) *retryable
- type BadRequestError
- type ErrDetail
- type ErrDetails
- type ErrOption
- type ErrSource
- type LimitExceededError
- type SentinelError
- type ShutdownError
- type StatusCodeWrapper
- type Violation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CancelWithError ¶
CancelWithError returns a context and a cancel function where the cancel function can override the error reported by the context.
This function is similar to context.WithCancel except that the cancel function can specify an error. Note that the cancel function can be called with nil args to make it behave exactly like context.WithCancel.
Example ¶
package main import ( "context" "fmt" "github.com/pkg/errors" "github.com/getoutreach/gobox/pkg/orerr" ) func main() { origErr := errors.New("something went wrong") shutdownErr := &orerr.ShutdownError{Err: origErr} ctx, cancel := orerr.CancelWithError(context.Background()) cancel(shutdownErr) fmt.Println("Err", ctx.Err()) }
Output: Err process has shutdown
func ExtractErrorMetadata ¶ added in v1.8.0
ExtractErrorMetadata returns any embedded grpc metadata in
func ExtractErrorStatusCategory ¶
func ExtractErrorStatusCategory(err error) statuscodes.StatusCategory
func ExtractErrorStatusCode ¶
func ExtractErrorStatusCode(err error) statuscodes.StatusCode
func IsErrorStatusCategory ¶
func IsErrorStatusCategory(err error, category statuscodes.StatusCategory) bool
func IsErrorStatusCode ¶
func IsErrorStatusCode(err error, code statuscodes.StatusCode) bool
func IsOneOf ¶
IsOneOf returns true if the supplied error is identical to an error supplied in the remaining function error arguments
Example ¶
package main import ( "context" "fmt" "io" "github.com/getoutreach/gobox/pkg/orerr" ) func main() { errList := []error{io.EOF, context.Canceled, context.DeadlineExceeded} if orerr.IsOneOf(io.EOF, errList...) { fmt.Println("io.EOF is part of the error list") } }
Output: io.EOF is part of the error list
func IsRetryable ¶
IsRetryable returns whether or not err is retryable.
func New ¶
New creates a new error wrapping all the error options onto it.
For convenience, if err is nil, this function returns nil.
Example ¶
package main import ( "fmt" "github.com/pkg/errors" "github.com/getoutreach/gobox/pkg/log" "github.com/getoutreach/gobox/pkg/orerr" ) func main() { origErr := errors.New("something went wrong") info := log.F{"hello": "world"} err := orerr.New(origErr, orerr.WithInfo(info), orerr.WithRetry()) formatted := log.F{} //nolint:errorlint // Why: test err.(log.Marshaler).MarshalLog(formatted.Set) fmt.Println("Err", err, orerr.IsRetryable(err), formatted) }
Output: Err something went wrong true map[hello:world]
func NewBadRequestError ¶ added in v1.64.0
NewBadRequestError return ready made intance of the BadRequestError error. It wraps given error with the BadRequest status
func NewErrDetails ¶ added in v1.81.0
NewErrDetails return ready made intance of the ErrDetails error. It wraps given error
func NewErrorStatus ¶
func NewErrorStatus(errToWrap error, errCode statuscodes.StatusCode) error
func Retryable ¶
func Retryable(err error) *retryable
Retryable wraps err in a type which denotes that the originating process is retryable.
Example ¶
process := func(attempt int) error { err := errors.New("something went wrong") if attempt < 2 { return Retryable(err) } return err } for i := 0; ; i++ { if err := process(i); err != nil { if IsRetryable(err) { fmt.Println("error is retryable") continue } fmt.Println("error is not retryable:", err) break } }
Output: error is retryable error is retryable error is not retryable: something went wrong
Types ¶
type BadRequestError ¶ added in v1.64.0
type BadRequestError struct { // Err is an original err Err error // Violations particular violations Violations []Violation }
BadRequestError represents an invalidate input error
func (BadRequestError) Error ¶ added in v1.64.0
func (e BadRequestError) Error() string
Error implements the err interface.
func (BadRequestError) Unwrap ¶ added in v1.64.0
func (e BadRequestError) Unwrap() error
Unwrap returns the inner error.
func (*BadRequestError) WithViolations ¶ added in v1.64.0
func (e *BadRequestError) WithViolations(violations ...Violation) *BadRequestError
WithViolations adds more violations into the error
type ErrDetail ¶ added in v1.81.0
type ErrDetail struct { ID string Title string Detail string Code *string Source *ErrSource Meta map[string]string }
ErrDetail describes detail of an error It's compatible with a single API error as per: https://jsonapi.org/format/#error-objects
func NewErrDetail ¶ added in v1.81.0
NewErrDetail creates a new intance of ErrDetail
func (ErrDetail) WithSourcePointer ¶ added in v1.81.0
WithSourcePointer allows to specify source pointer
type ErrDetails ¶ added in v1.81.0
type ErrDetails struct { // Details is the list of error details Details []ErrDetail // contains filtered or unexported fields }
ErrDetails represents an error with list of details
func (ErrDetails) Error ¶ added in v1.81.0
func (e ErrDetails) Error() string
Error implements the err interface.
func (ErrDetails) Unwrap ¶ added in v1.81.0
func (e ErrDetails) Unwrap() error
Unwrap returns the inner error.
func (*ErrDetails) WithDetails ¶ added in v1.81.0
func (e *ErrDetails) WithDetails(details ...ErrDetail) *ErrDetails
WithDetails adds more details into the error
type ErrOption ¶
ErrOption just wraps an error.
func WithDetails ¶ added in v1.81.0
WithDetails calls NewErrorDetails with the given error details.
It is a functional option for use with New.
func WithInfo ¶
WithInfo attaches the provided logs to the error.
It is a functional option for use with New.
func WithMeta ¶ added in v1.8.0
WithMeta attaches the provided grpc metadata to the error.
It is a functional option for use with New.
func WithRetry ¶
func WithRetry() ErrOption
WithRetry marks the error as retryable.
It is a functional option for use with New.
func WithStatus ¶ added in v1.8.0
func WithStatus(code statuscodes.StatusCode) ErrOption
WithStatus calls NewErrorStatus with the given code.
It is a functional option for use with New.
type ErrSource ¶ added in v1.81.0
type ErrSource struct {
Pointer string
}
ErrSource describes source location of the error
type LimitExceededError ¶
type LimitExceededError struct { // Kind refers to the kind of rate whose limit has been exceeded. Kind string Err error }
LimitExceededError indicates some limit has exceeded. The actual limit that has exceeded is indicated via the Kind field. An inner error may be provided via Err.
func (LimitExceededError) Error ¶
func (e LimitExceededError) Error() string
Error implements the err interface.
func (LimitExceededError) Unwrap ¶
func (e LimitExceededError) Unwrap() error
Unwrap returns the inner error.
type SentinelError ¶
type SentinelError string
A SentinelError is a constant which ought to be compared using errors.Is.
Example ¶
package main import ( "errors" "fmt" "github.com/getoutreach/gobox/pkg/orerr" ) const ErrUsernameTaken orerr.SentinelError = "username already taken" func createUser(_ string) error { return ErrUsernameTaken } func main() { if err := createUser("joe"); err != nil { if errors.Is(err, ErrUsernameTaken) { fmt.Println("User 'joe' already exists") return } panic(err) } }
Output: User 'joe' already exists
type ShutdownError ¶
type ShutdownError struct {
Err error
}
ShutdownError incidates the process is shutting down. An inner error may be provided via Err.
func (ShutdownError) Error ¶
func (e ShutdownError) Error() string
Error implements the err interface.
type StatusCodeWrapper ¶
type StatusCodeWrapper struct {
// contains filtered or unexported fields
}
func (*StatusCodeWrapper) Error ¶
func (w *StatusCodeWrapper) Error() string
func (*StatusCodeWrapper) StatusCategory ¶
func (w *StatusCodeWrapper) StatusCategory() statuscodes.StatusCategory
func (*StatusCodeWrapper) StatusCode ¶
func (w *StatusCodeWrapper) StatusCode() statuscodes.StatusCode
func (*StatusCodeWrapper) Unwrap ¶
func (w *StatusCodeWrapper) Unwrap() error
type Violation ¶ added in v1.64.0
type Violation struct { // Field describes the field path with request object graph. // When not provided the violation is related to a whole entitity. Field *string // Domain is optional identifier of the domain name of the validation subject. Domain *string // Reason of the error. In most of the cases the validation rule indentifier. Reason string // Additional structured details about this error. Could be used for localization of the error. Metadata map[string]string }
Violation describe particular input violation. It might be field and domain specific.
func NewViolation ¶ added in v1.64.0
NewViolation creates a new intance of Violation
func (Violation) WithDomain ¶ added in v1.64.0
WithField allows to specify domain name of the field that violation belongs to