errors

package
v0.0.0-...-d36796e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 4 Imported by: 2

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

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 Assert

func Assert(exprs ...bool)

Assert panics if any of the exprs is false.

func Check

func Check(values ...any)

Check panics with the last error in values if it's not nil.

func Check1

func Check1[T1 any](in1 T1, err error) (out1 T1)

Check1 returns the in1 if err is nil and panics with err otherwise.

func Check2

func Check2[T1, T2 any](in1 T1, in2 T2, err error) (out1 T1, out2 T2)

Check2 returns the in1 and in2 if err is nil and panics with err otherwise.

func Check3

func Check3[T1, T2, T3 any](in1 T1, in2 T2, in3 T3, err error) (out1 T1, out2 T2, out3 T3)

Check3 returns the in1, in2 and in3 if err is nil and panics with err otherwise.

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

func Is(err, target error) bool

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

func Join(errs ...error) error

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 Rethrow

func Rethrow(err error)

func Throw

func Throw(key error, errs ...error)

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.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

It's a drop-in replacement of `errors.Unwrap` defined in Go1.20+ standard package. See documents in `errors.Unwrap` for more details.

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.

func New

func New(msg string) Error

New returns a new Error interface with the given message.

type ErrorCode

type ErrorCode[T any] interface {
	Error

	Code() T
}

ErrorCode is an error with error code.

func NewErrorCode

func NewErrorCode[T any](code T, msg string) ErrorCode[T]

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

func If(values ...any) Thrower

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

type Thrower1[T1 any] interface {
	Throw(err error) T1
}

Thrower1 wraps original function call results with error handling.

func If1

func If1[T1 any](in1 T1, err error) Thrower1[T1]

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

type Thrower2[T1, T2 any] interface {
	Throw(err error) (T1, T2)
}

Thrower2 wraps original function call results with error handling.

func If2

func If2[T1, T2 any](in1 T1, in2 T2, err error) Thrower2[T1, T2]

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

type Thrower3[T1, T2, T3 any] interface {
	Throw(err error) (T1, T2, T3)
}

Thrower3 wraps original function call results with error handling.

func If3

func If3[T1, T2, T3 any](in1 T1, in2 T2, in3 T3, err error) Thrower3[T1, T2, T3]

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

type Thrower4[T1, T2, T3, T4 any] interface {
	Throw(err error) (T1, T2, T3, T4)
}

Thrower4 wraps original function call results with error handling.

func If4

func If4[T1, T2, T3, T4 any](in1 T1, in2 T2, in3 T3, in4 T4, err error) Thrower4[T1, T2, T3, T4]

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

type Thrower5[T1, T2, T3, T4, T5 any] interface {
	Throw(err error) (T1, T2, T3, T4, T5)
}

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

type Thrower6[T1, T2, T3, T4, T5, T6 any] interface {
	Throw(err error) (T1, T2, T3, T4, T5, T6)
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL