Documentation ¶
Overview ¶
Package eris provides a better way to handle, trace, and log errors in Go.
Index ¶
- func As(err error, target interface{}) bool
- func Cause(err error) error
- func Errorf(format string, args ...interface{}) error
- func Is(err, target error) bool
- func New(msg string) error
- func StackFrames(err error) []uintptr
- func ToCustomJSON(err error, format JSONFormat) map[string]interface{}
- func ToCustomString(err error, format StringFormat) string
- func ToJSON(err error, withTrace bool) map[string]interface{}
- func ToString(err error, withTrace bool) string
- func Unwrap(err error) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type ErrLink
- type ErrRoot
- type FormatOptions
- type JSONFormat
- type Stack
- type StackFrame
- type StringFormat
- type UnpackedError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.5.0
As finds the first error in err's chain that matches target. If there's a match, it sets target to that error value and returns true. Otherwise, it returns false.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true.
func Cause ¶
Cause returns the root cause of the error, which is defined as the first error in the chain. The original error is returned if it does not implement `Unwrap() error` and nil is returned if the error is nil.
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.
func StackFrames ¶ added in v0.2.0
StackFrames returns the trace of an error in the form of a program counter slice. Use this method if you want to pass the eris stack trace to some other error tracing library.
func ToCustomJSON ¶ added in v0.2.0
func ToCustomJSON(err error, format JSONFormat) map[string]interface{}
ToCustomJSON returns a JSON formatted map for a given error.
To declare custom format, the Format object has to be passed as an argument. An error without trace will be formatted as follows:
{ "root": { "message": "Root error msg", }, "wrap": [ { "message": "Wrap error msg'", } ] }
An error with trace will be formatted as follows:
{ "root": { "message": "Root error msg", "stack": [ "<Method2>[Format.StackElemSep]<File2>[Format.StackElemSep]<Line2>", "<Method1>[Format.StackElemSep]<File1>[Format.StackElemSep]<Line1>" ] } "wrap": [ { "message": "Wrap error msg", "stack": "<Method2>[Format.StackElemSep]<File2>[Format.StackElemSep]<Line2>" } ] }
func ToCustomString ¶ added in v0.2.0
func ToCustomString(err error, format StringFormat) string
ToCustomString returns a custom formatted string for a given error.
To declare custom format, the Format object has to be passed as an argument. An error without trace will be formatted as follows:
<Wrap error msg>[Format.ErrorSep]<Root error msg>
An error with trace will be formatted as follows:
<Wrap error msg>[Format.MsgStackSep] [Format.PreStackSep]<Method2>[Format.StackElemSep]<File2>[Format.StackElemSep]<Line2>[Format.ErrorSep] <Root error msg>[Format.MsgStackSep] [Format.PreStackSep]<Method2>[Format.StackElemSep]<File2>[Format.StackElemSep]<Line2>[Format.ErrorSep] [Format.PreStackSep]<Method1>[Format.StackElemSep]<File1>[Format.StackElemSep]<Line1>[Format.ErrorSep]
func ToJSON ¶ added in v0.2.0
ToJSON returns a JSON formatted map for a given error.
An error without trace will be formatted as follows:
{ "root": [ { "message": "Root error msg" } ], "wrap": { "message": "Wrap error msg" } }
An error with trace will be formatted as follows:
{ "root": [ { "message": "Root error msg", "stack": [ "<Method2>:<File2>:<Line2>", "<Method1>:<File1>:<Line1>" ] } ], "wrap": { "message": "Wrap error msg", "stack": "<Method2>:<File2>:<Line2>" } }
Example (External) ¶
Demonstrates JSON formatting of wrapped errors that originate from external (non-eris) error types.
package main import ( "encoding/json" "fmt" "io" "github.com/rotisserie/eris" ) func main() { // example func that returns an IO error readFile := func(fname string) error { return io.ErrUnexpectedEOF } // unpack and print the error err := readFile("example.json") u, _ := json.Marshal(eris.ToJSON(err, false)) // false: omit stack trace fmt.Println(string(u)) // example output: // { // "external":"unexpected EOF" // } }
Output:
Example (Global) ¶
Demonstrates JSON formatting of wrapped errors that originate from global root errors.
package main import ( "encoding/json" "fmt" "github.com/rotisserie/eris" ) var ErrUnexpectedEOF = eris.New("unexpected EOF") func main() { // example func that wraps a global error value readFile := func(fname string) error { return eris.Wrapf(ErrUnexpectedEOF, "error reading file '%v'", fname) // line 6 } // example func that catches and returns an error without modification parseFile := func(fname string) error { // read the file err := readFile(fname) // line 12 if err != nil { return err } return nil } // unpack and print the error via uerr.ToJSON(...) err := parseFile("example.json") // line 20 u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t") // true: include stack trace fmt.Printf("%v\n", string(u)) // example output: // { // "root": { // "message": "unexpected EOF", // "stack": [ // "main.main:.../example/main.go:20", // "main.parseFile:.../example/main.go:12", // "main.readFile:.../example/main.go:6" // ] // }, // "wrap": [ // { // "message": "error reading file 'example.json'", // "stack": "main.readFile:.../example/main.go:6" // } // ] // } }
Output:
Example (Local) ¶
Demonstrates JSON formatting of wrapped errors that originate from local root errors (created at the source of the error via eris.New).
package main import ( "encoding/json" "fmt" "github.com/rotisserie/eris" ) func main() { // example func that returns an eris error readFile := func(fname string) error { return eris.New("unexpected EOF") // line 3 } // example func that catches an error and wraps it with additional context parseFile := func(fname string) error { // read the file err := readFile(fname) // line 9 if err != nil { return eris.Wrapf(err, "error reading file '%v'", fname) // line 11 } return nil } // example func that just catches and returns an error processFile := func(fname string) error { // parse the file err := parseFile(fname) // line 19 if err != nil { return err } return nil } // another example func that catches and wraps an error printFile := func(fname string) error { // process the file err := processFile(fname) // line 29 if err != nil { return eris.Wrapf(err, "error printing file '%v'", fname) // line 31 } return nil } // unpack and print the raw error err := printFile("example.json") // line 37 u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t") fmt.Printf("%v\n", string(u)) // example output: // { // "root": { // "message": "unexpected EOF", // "stack": [ // "main.main:.../example/main.go:37", // "main.printFile:.../example/main.go:31", // "main.printFile:.../example/main.go:29", // "main.processFile:.../example/main.go:19", // "main.parseFile:.../example/main.go:11", // "main.parseFile:.../example/main.go:9", // "main.readFile:.../example/main.go:3" // ] // }, // "wrap": [ // { // "message": "error printing file 'example.json'", // "stack": "main.printFile:.../example/main.go:31" // }, // { // "message": "error reading file 'example.json'", // "stack": "main.parseFile: .../example/main.go: 11" // } // ] // } }
Output:
func ToString ¶ added in v0.2.0
ToString returns a default formatted string for a given error.
An error without trace will be formatted as follows:
<Wrap error msg>: <Root error msg>
An error with trace will be formatted as follows:
<Wrap error msg> <Method2>:<File2>:<Line2> <Root error msg> <Method2>:<File2>:<Line2> <Method1>:<File1>:<Line1>
Example (External) ¶
Demonstrates string formatting of wrapped errors that originate from external (non-eris) error types.
package main import ( "fmt" "io" "github.com/rotisserie/eris" ) func main() { // example func that returns an IO error readFile := func(fname string) error { return io.ErrUnexpectedEOF } // unpack and print the error err := readFile("example.json") fmt.Println(eris.ToString(err, false)) // false: omit stack trace // example output: // unexpected EOF }
Output:
Example (Global) ¶
Demonstrates string formatting of wrapped errors that originate from global root errors.
package main import ( "fmt" "github.com/rotisserie/eris" ) var FormattedErrUnexpectedEOF = eris.Errorf("unexpected %v", "EOF") func main() { // example func that wraps a global error value readFile := func(fname string) error { return eris.Wrapf(FormattedErrUnexpectedEOF, "error reading file '%v'", fname) // line 6 } // example func that catches and returns an error without modification parseFile := func(fname string) error { // read the file err := readFile(fname) // line 12 if err != nil { return err } return nil } // example func that just catches and returns an error processFile := func(fname string) error { // parse the file err := parseFile(fname) // line 22 if err != nil { return eris.Wrapf(err, "error processing file '%v'", fname) // line 24 } return nil } // call processFile and catch the error err := processFile("example.json") // line 30 // print the error via fmt.Printf fmt.Printf("%v\n", err) // %v: omit stack trace // example output: // unexpected EOF: error reading file 'example.json' // unpack and print the error via uerr.ToString(...) fmt.Printf("%v\n", eris.ToString(err, true)) // true: include stack trace // example output: // error reading file 'example.json' // main.readFile:.../example/main.go:6 // unexpected EOF // main.main:.../example/main.go:30 // main.processFile:.../example/main.go:24 // main.processFile:.../example/main.go:22 // main.parseFile:.../example/main.go:12 // main.readFile:.../example/main.go:6 }
Output:
Example (Local) ¶
Demonstrates string formatting of wrapped errors that originate from local root errors (created at the source of the error via eris.New).
package main import ( "fmt" "github.com/rotisserie/eris" ) func main() { // example func that returns an eris error readFile := func(fname string) error { return eris.New("unexpected EOF") // line 3 } // example func that catches an error and wraps it with additional context parseFile := func(fname string) error { // read the file err := readFile(fname) // line 9 if err != nil { return eris.Wrapf(err, "error reading file '%v'", fname) // line 11 } return nil } // call parseFile and catch the error err := parseFile("example.json") // line 17 // print the error via fmt.Printf fmt.Printf("%v\n", err) // %v: omit stack trace // example output: // unexpected EOF: error reading file 'example.json' // unpack and print the error via uerr.ToString(...) fmt.Println(eris.ToString(err, true)) // true: include stack trace // example output: // error reading file 'example.json' // main.parseFile:.../example/main.go:11 // unexpected EOF // main.main:.../example/main.go:17 // main.parseFile:.../example/main.go:11 // main.parseFile:.../example/main.go:9 // main.readFile:.../example/main.go:3 }
Output:
func Unwrap ¶
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.
func Wrap ¶
Wrap adds additional context to all error types while maintaining the type of the original error.
This method behaves differently for each error type. For root errors, the stack trace is reset to the current callers which ensures traces are correct when using global/sentinel error values. Wrapped error types are simply wrapped with the new context. For external types (i.e. something other than root or wrap errors), this method attempts to unwrap them while building a new error chain. If an external type does not implement the unwrap interface, it flattens the error and creates a new root error from it before wrapping with the additional context.
Types ¶
type ErrLink ¶
type ErrLink struct { Msg string Frame StackFrame }
ErrLink represents a single error frame and the accompanying message.
type FormatOptions ¶ added in v0.3.0
type FormatOptions struct { InvertOutput bool // Flag that inverts the error output (wrap errors shown first). WithTrace bool // Flag that enables stack trace output. InvertTrace bool // Flag that inverts the stack trace output (top of call stack shown first). WithExternal bool // Flag that enables external error output. }
FormatOptions defines output options like omitting stack traces and inverting the error or stack order.
type JSONFormat ¶ added in v0.3.0
type JSONFormat struct { Options FormatOptions // Format options (e.g. omitting stack trace or inverting the output order). // todo: maybe allow setting of wrap/root keys in the output map as well StackElemSep string // Separator between elements of each stack frame. }
JSONFormat defines a JSON error format.
func NewDefaultJSONFormat ¶ added in v0.3.0
func NewDefaultJSONFormat(options FormatOptions) JSONFormat
NewDefaultJSONFormat returns a default JSON output format.
type Stack ¶ added in v0.2.0
type Stack []StackFrame
Stack is an array of stack frames stored in a human readable format.
type StackFrame ¶
StackFrame stores a frame's runtime information in a human readable format.
type StringFormat ¶ added in v0.3.0
type StringFormat struct { Options FormatOptions // Format options (e.g. omitting stack trace or inverting the output order). MsgStackSep string // Separator between error messages and stack frame data. PreStackSep string // Separator at the beginning of each stack frame. StackElemSep string // Separator between elements of each stack frame. ErrorSep string // Separator between each error in the chain. }
StringFormat defines a string error format.
func NewDefaultStringFormat ¶ added in v0.3.0
func NewDefaultStringFormat(options FormatOptions) StringFormat
NewDefaultStringFormat returns a default string output format.
type UnpackedError ¶
UnpackedError represents complete information about an error.
This type can be used for custom error logging and parsing. Use `eris.Unpack` to build an UnpackedError from any error type. The ErrChain and ErrRoot fields correspond to `wrapError` and `rootError` types, respectively. If any other error type is unpacked, it will appear in the ExternalErr field.
func Unpack ¶
func Unpack(err error) UnpackedError
Unpack returns a human-readable UnpackedError type for a given error.