Documentation
¶
Overview ¶
Package errors provides utilities to create and handle all kinds errors. It's recommended to use this package to replace any use of standard package "errors".
The error handler design is imspired by Go2 error handling proposal. See https://github.com/golang/proposal/blob/master/design/go2draft-error-handling-overview.md.
Index ¶
- func As(err error, target any) bool
- func Assert(exprs ...bool)
- func Check(values ...any)
- func Check1[T1 any](in1 T1, err error) (out1 T1)
- func Check2[T1, T2 any](in1 T1, in2 T2, err error) (out1 T1, out2 T2)
- func Check3[T1, T2, T3 any](in1 T1, in2 T2, in3 T3, err error) (out1 T1, out2 T2, out3 T3)
- func Check4[T1, T2, T3, T4 any](in1 T1, in2 T2, in3 T3, in4 T4, err error) (out1 T1, out2 T2, out3 T3, out4 T4)
- func Check5[T1, T2, T3, T4, T5 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5)
- func Check6[T1, T2, T3, T4, T5, T6 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6)
- func Check7[T1, T2, T3, T4, T5, T6, T7 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6, out7 T7)
- func Check8[T1, T2, T3, T4, T5, T6, T7, T8 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, in8 T8, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6, out7 T7, out8 T8)
- func Handle(target *error)
- func Is(err, target error) bool
- func Join(errs ...error) error
- func Rethrow(err error)
- func Throw(key error, errs ...error)
- func Throwf(format string, args ...interface{})
- func Unwrap(err error) error
- type Error
- type ErrorCode
- type HandlerError
- type Thrower
- type Thrower1
- type Thrower2
- type Thrower3
- type Thrower4
- type Thrower5
- type Thrower6
- type Thrower7
- type Thrower8
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
It's a drop-in replacement of `errors.As` defined in Go1.20+ standard package. See documents in `errors.As` for more details.
func Check4 ¶
func Check4[T1, T2, T3, T4 any](in1 T1, in2 T2, in3 T3, in4 T4, err error) (out1 T1, out2 T2, out3 T3, out4 T4)
Check4 returns the in1, in2, in3 and in4 if err is nil and panics with err otherwise.
func Check5 ¶
func Check5[T1, T2, T3, T4, T5 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5)
Check5 returns the in1, in2, in3, in4 and in5 if err is nil and panics with err otherwise.
func Check6 ¶
func Check6[T1, T2, T3, T4, T5, T6 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6)
Check6 returns the in1, in2, in3, in4, in5 and in6 if err is nil and panics with err otherwise.
func Check7 ¶
func Check7[T1, T2, T3, T4, T5, T6, T7 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6, out7 T7)
Check7 returns the in1, in2, in3, in4, in5, in6 and in7 if err is nil and panics with err otherwise.
func Check8 ¶
func Check8[T1, T2, T3, T4, T5, T6, T7, T8 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, in8 T8, err error) (out1 T1, out2 T2, out3 T3, out4 T4, out5 T5, out6 T6, out7 T7, out8 T8)
Check8 returns the in1, in2, in3, in4, in5, in6, in7 and in8 if err is nil and panics with err otherwise.
func Handle ¶
func Handle(target *error)
Handle recovers from panic and returns error to the target if panic is an error.
func Is ¶
Is reports whether any error in err's tree matches target.
It's a drop-in replacement of `errors.Is` defined in Go1.20+ standard package. See documents in `errors.Is` for more details.
func Join ¶
Join returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if errs contains no non-nil values. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.
It's a drop-in replacement of `errors.Join` defined in Go1.20+ standard package. See documents in `errors.Join` for more details.
func Throw ¶
Throw joins all errs to one and panics with it.
The first element of errs is the key error. If the key error is nil, Throw does nothing.
func Throwf ¶
func Throwf(format string, args ...interface{})
Throwf uses fmt.Errorf to format the error message and panics with it.
Types ¶
type Error ¶
type Error interface { error // If the last element of values is an error, it will be thrown. // The thrown error will be this Error interface wrapping the last element of values as its cause. Check(values ...any) }
Error is an interface that wraps the error interface.
type ErrorCode ¶
ErrorCode is an error with error code.
func NewErrorCode ¶
NewErrorCode returns a new ErrorCode interface with the given code and message.
type HandlerError ¶
type HandlerError interface { error Unwrap() []error // KeyError returns the key error which will be considered as the major reason that causes error. // When Shana reports error message to client, the key error will be used as error message. KeyError() error }
HandlerError is an error containing several sub errors.
type Thrower ¶
type Thrower interface {
Throw(err error)
}
Thrower wraps original function call results with error handling.
func If ¶
If checks the error value returned by a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check instead.
type Thrower1 ¶
Thrower1 wraps original function call results with error handling.
func If1 ¶
If1 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If1 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check1 instead.
type Thrower2 ¶
Thrower2 wraps original function call results with error handling.
func If2 ¶
If2 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If2 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check2 instead.
type Thrower3 ¶
Thrower3 wraps original function call results with error handling.
func If3 ¶
If3 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If3 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check3 instead.
type Thrower4 ¶
Thrower4 wraps original function call results with error handling.
func If4 ¶
If4 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If4 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check4 instead.
type Thrower5 ¶
Thrower5 wraps original function call results with error handling.
func If5 ¶
func If5[T1, T2, T3, T4, T5 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, err error) Thrower5[T1, T2, T3, T4, T5]
If5 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If5 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check5 instead.
type Thrower6 ¶
Thrower6 wraps original function call results with error handling.
func If6 ¶
func If6[T1, T2, T3, T4, T5, T6 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, err error) Thrower6[T1, T2, T3, T4, T5, T6]
If6 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If6 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check6 instead.
type Thrower7 ¶
type Thrower7[T1, T2, T3, T4, T5, T6, T7 any] interface { Throw(err error) (T1, T2, T3, T4, T5, T6, T7) }
Thrower7 wraps original function call results with error handling.
func If7 ¶
func If7[T1, T2, T3, T4, T5, T6, T7 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, err error) Thrower7[T1, T2, T3, T4, T5, T6, T7]
If7 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If7 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check7 instead.
type Thrower8 ¶
type Thrower8[T1, T2, T3, T4, T5, T6, T7, T8 any] interface { Throw(err error) (T1, T2, T3, T4, T5, T6, T7, T8) }
Thrower8 wraps original function call results with error handling.
func If8 ¶
func If8[T1, T2, T3, T4, T5, T6, T7, T8 any](in1 T1, in2 T2, in3 T3, in4 T4, in5 T5, in6 T6, in7 T7, in8 T8, err error) Thrower8[T1, T2, T3, T4, T5, T6, T7, T8]
If8 stores the return value of a function call and returns a thrower. If the function call returns an error, calling thrower's Throw(err) will join err with the error.
If8 is useful when we want to return a business error, e.g. error code, with a cause. If it's not the case, use Check8 instead.