Documentation ¶
Overview ¶
Package failure provides an error represented as error code and extensible error interface with wrappers.
Index ¶
- func CauseOf(err error) error
- func Custom(err error, wrappers ...Wrapper) error
- func Is(err error, codes ...Code) bool
- func MarkUnexpected(err error, wrappers ...Wrapper) error
- func MessageOf(err error) (string, bool)
- func New(code Code, wrappers ...Wrapper) error
- func Trace(err error, vs Tracer)
- func Translate(err error, code Code, wrappers ...Wrapper) error
- func Unexpected(msg string, wrappers ...Wrapper) error
- func Wrap(err error, wrappers ...Wrapper) error
- type CallStack
- type Code
- type Context
- type Contexter
- type Frame
- type Iterator
- type Message
- type Messenger
- type StringCode
- type StringTracer
- type Tracer
- type Wrapper
- type WrapperFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Custom ¶ added in v0.7.0
Custom is the general error wrapping constructor. It just wraps err with given wrappers.
func MarkUnexpected ¶ added in v0.12.1
MarkUnexpected wraps err and preventing propagation of error code from underlying error. It is used where an error can be returned but expecting it does not happen. The returned error does not return error code from function CodeOf.
func Translate ¶
Translate translates the err to an error with given code. It wraps the error with given wrappers, and automatically add call stack and formatter.
func Unexpected ¶ added in v0.9.0
Unexpected creates an error from message without error code. The returned error should be kind of internal or unknown error.
Types ¶
type CallStack ¶
type CallStack interface { // HeadFrame returns a frame of where call stack is created. // This is same as Frames()[0], but uses memory more efficiently. HeadFrame() Frame // Frames returns entire frames of the call stack. Frames() []Frame }
CallStack represents a call stack. The call stack includes information where the occurred and how the function was called.
func CallStackOf ¶
CallStackOf extracts a call stack from the err. Returned call stack is for the most deepest place (appended first).
func NewCallStack ¶ added in v0.9.4
NewCallStack returns call stack from program counters. You can use Callers for usual usage.
type Code ¶
type Code interface { // ErrorCode returns an error code in string representation. ErrorCode() string }
Code represents an error code of an error. The code should be able to be compared by == operator. Basically, it should to be defined as constants.
You can also define your own code type instead of using StringCode type, when you want to distinguish errors by type for some purpose (e.g. define code type for each package like user, item, auth etc).
type Context ¶ added in v0.11.0
Context is a key-value data which describes the how the error occurred for debugging purpose. You must not use context data as a part of your application logic. Just print it. If you want to extract Context from error for printing purpose, you can define an interface with method `GetContext() Context` and use it with iterator, like other extraction functions (see: MessageOf).
type Contexter ¶ added in v1.1.0
type Contexter interface {
Context() Context
}
Contexter is an interface to be used with errors.As.
type Frame ¶ added in v0.2.0
type Frame interface { // Path returns a absolute path to the file. Path() string // File returns a file name. File() string // Line returns a line number in the file. Line() int // Func returns a function name. Func() string // Pkg returns a package name of the function. Pkg() string // PkgPath returns a path-qualified package name of the function. PkgPath() string // PC returns a program counter of this frame. PC() uintptr }
Frame represents a stack frame.
type Iterator ¶ added in v0.7.0
type Iterator struct {
// contains filtered or unexported fields
}
Iterator is designed to iterate wrapped errors with for loop.
func NewIterator ¶ added in v0.7.0
NewIterator creates an iterator for the err.
func (*Iterator) As ¶ added in v0.12.0
As tries to extract data from current error. It returns true if the current error implemented As and it returned true.
type Message ¶ added in v0.5.0
type Message string
Message is a wrapper which appends message to an error.
type Messenger ¶ added in v1.1.0
type Messenger interface {
Message() string
}
Messenger is an interface to be used with errors.As.
type StringCode ¶ added in v0.2.0
type StringCode string
StringCode represents an error Code in string.
func (StringCode) ErrorCode ¶ added in v0.2.0
func (c StringCode) ErrorCode() string
ErrorCode implements the Code interface.
type StringTracer ¶ added in v0.14.0
type StringTracer []string
func (*StringTracer) Push ¶ added in v0.14.0
func (st *StringTracer) Push(v interface{})
type Wrapper ¶ added in v0.7.0
type Wrapper interface { // WrapError should wrap given err to append some // capability to the error. WrapError(err error) error }
Wrapper interface is used by constructor functions.
func WithCallStackSkip ¶ added in v0.7.0
WithCallStackSkip appends a call stack to the err skipping first N frames. You don't have to use this directly, unless using function Custom.
func WithCode ¶ added in v0.9.0
WithCode appends code to an error. You don't have to use this directly, unless using function Custom. Basically, you can use function Translate instead of this.
func WithFormatter ¶ added in v0.7.0
func WithFormatter() Wrapper
WithFormatter appends an error formatter to the err.
%v+: Print trace for each place, and deepest call stack. %#v: Print raw structure of the error. others (%s, %v): Same as err.Error().
You don't have to use this directly, unless using function Custom.
func WithUnexpected ¶ added in v0.13.0
func WithUnexpected() Wrapper
WithUnexpected wraps the err to mark it is unexpected. You don't have to use this directly, unless using function Custom. Please use Unexpected or MarkUnexpected.
type WrapperFunc ¶ added in v0.7.0
WrapperFunc is an adaptor to use function as the Wrapper interface.
func (WrapperFunc) WrapError ¶ added in v0.7.0
func (f WrapperFunc) WrapError(err error) error
WrapError implements the Wrapper interface.