Documentation ¶
Overview ¶
Package e is an error-handling package designed to be simple, safe, and compatible.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorCode ¶
ErrorCode returns the first unwrapped Code of an error which implements ClientFacing interface. Otherwise returns an empty string.
func ErrorMessage ¶
ErrorMessage returns the first unwrapped Message of an error which implements ClientFacing interface. Otherwise returns an empty string.
func ErrorStacktrace ¶ added in v1.0.0
ErrorStacktrace returns the innermost Stack of an error which implements HasStacktrace interface. Otherwise returns an empty string.
Types ¶
type ClientFacing ¶
type ClientFacing interface { // ClientCode returns the a short string representing the type of error, such as // "database_error", to be used by a client or an application. // // Note: ErrorCode() should be used to retrieve the topmost Code() ClientCode() string // ClientMessage returns a user-friendly error message (if any) which is logically // separate from the error cause. // // Note: ErrorMessage() should be used to retrieve the topmost Message(). ClientMessage() string }
ClientFacing allows custom error types to be used with utility functions ErrorCode() and ErrorMessage().
type Error ¶
type Error interface { error ClientFacing HasStacktrace Unwrap() error // SetCode adds an error type to a non-nil Error such as "unexpected_error", // "database_error", "not_exists", etc. // // Will panic when used with a nil Error receiver. SetCode(code string) Error // SetMessage adds a user-friendly message to a non-nil Error. // Message will not be printed with Error() and should be retrieved with ErrorMessage(). // // Will panic when used with a nil Error receiver. SetMessage(message string) Error }
Error represents a standard application error. Implements ClientFacing and HasStacktrace so it can be introspected with functions like ErrorCode, ErrorMessage, and ErrorStacktrace.
func NewError ¶ added in v1.0.0
NewError constructs a new Error. code should be a short, single string describing the type of error (typically a pre-defined const). cause is used to create the nested error which will act as the root of the error stack.
Usage:
func Foo(bar *Bar) error { if bar == nil { return e.New("unexpected_error", "bar is nil") } return doFoo(bar) }
func NewErrorf ¶ added in v1.0.0
NewErrorf constructs a new Error with formatted string. code should be a short, single string describing the type of error (typically a pre-defined const). cause is used to create the nested error which will act as the root of the error stack.
Usage:
func Foo(bar Bar) error { done := doFoo(bar) if !done { return e.NewErrorf("unexpected_error", "cannot process bar: %v", bar) } return nil }
func Wrap ¶
Wrap adds the name of the calling function to the wrapped error. OptionalInfo can be passed to insert more context at the wrap site. Only the first OptionalInfo string will be used.
Basic usage:
err := Foo() if err != nil { return e.Wrap(err) }
Adding additional info:
err := Foo() if err != nil { return e.Wrap(err, fmt.Sprintf("cannot find id: %v", id)) }
type HasStacktrace ¶ added in v1.0.0
type HasStacktrace interface { // Stacktrace returns the innermost stacktrace, if any. Stacktrace() string }
HasStacktrace allows custom error types to be used with utility function ErrorStacktrace().