Documentation ¶
Overview ¶
Package zerrors provides error wrapping functionality.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Detail ¶ added in v0.2.0
Detail is an alternative error encoding to err.Error(), equivalent to the %+v format form. It can be called on any error but for non-zerror errors is encodes it in the standard %+v fmt form. For zerror errors, it uses the 'detail' function to serialise and it and is expected to (and by default, is) more detailed and expensive than err.Error() and contains frame information. By default this is in the form: "{err1} ({file}:{line}): {err2} ({file}:{line}): ... : {errN} ({file}:{line})".
func Frame ¶
Frame returns the frame contained at this particular node in the error chain. For non-wrapError errors or wrapError errors without a frame, it returns an empty frame and false. Otherwise it returns the populated frame and true.
func New ¶
New produces a wrapError from a non-wrapError. If frame capture is enabled, it will get one at the caller of New. If err is a wrapError it returns itself, with no frame changes. This should be avoided.
func SNew ¶
SNew produces a wrapError with a simple string error. If frame capture is enabled, it will get one at the caller of SNew.
func SWrap ¶
SWrap produces a wrapError using a simple string error. The first argument is the error being wrapped, the second is the string error being added to it. If the first error is nil, Wrap does nothing and returns nil. Otherwise it returns a wrapError. If frame capture is enabled, it will get one at the caller of SWrap.
Example ¶
package main import ( "errors" "fmt" "path" "strings" "github.com/JavierZunzunegui/zerrors" ) func main() { err := zerrors.SNew("base") err = zerrors.SWrap(err, "first wrapper") fmt.Println(err) // basic standard format fmt.Println(zerrors.Detail(err)) // detail standard format fmt.Println(basicCustomErrorFormat(err)) // basic custom format fmt.Println(detailCustomErrorFormat(err)) // detail custom format } // An example custom alternative to err.Error(). func basicCustomErrorFormat(err error) string { var ss []string for ; err != nil; err = errors.Unwrap(err) { ss = append(ss, zerrors.Value(err).Error()) } return strings.Join(ss, " - ") } // An example custom alternative to zerrors.Detail(err). func detailCustomErrorFormat(err error) string { var ss []string for ; err != nil; err = errors.Unwrap(err) { s := zerrors.Value(err).Error() if frame, ok := zerrors.Frame(err); ok { s += " (" + path.Base(frame.Function) + ")" } ss = append(ss, s) } return strings.Join(ss, " - ") }
Output: first wrapper: base first wrapper (error_example_test.go:28): base (error_example_test.go:27) first wrapper - base first wrapper (zerrors_test.ExampleSWrap) - base (zerrors_test.ExampleSWrap)
func Value ¶
Value returns the error contained at this particular node in the error chain. For non-wrapError errors, it returns itself. The return value is never a wrapError, and is only nil if the input is also nil.
func Wrap ¶
Wrap produces a wrapError using any error type. The first argument is the error being wrapped, the second is the error being added to it. If the first error is nil, Wrap does nothing and returns nil. If the second error is nil, it returns the first error. This should be avoided. Otherwise it returns a wrapError. If frame capture is enabled, it will get one at the caller of Wrap.
Example ¶
package main import ( "errors" "fmt" "path" "strings" "github.com/JavierZunzunegui/zerrors" ) type codeError struct { code int } func (e codeError) Error() string { return fmt.Sprintf("code=%d", e.code) } func main() { err := zerrors.SNew("base") err = zerrors.Wrap(err, codeError{1}) fmt.Println(err) // basic standard format fmt.Println(zerrors.Detail(err)) // detail standard format fmt.Println(basicCustomErrorFormat(err)) // basic custom format fmt.Println(detailCustomErrorFormat(err)) // detail custom format } // An example custom alternative to err.Error(). func basicCustomErrorFormat(err error) string { var ss []string for ; err != nil; err = errors.Unwrap(err) { ss = append(ss, zerrors.Value(err).Error()) } return strings.Join(ss, " - ") } // An example custom alternative to zerrors.Detail(err). func detailCustomErrorFormat(err error) string { var ss []string for ; err != nil; err = errors.Unwrap(err) { s := zerrors.Value(err).Error() if frame, ok := zerrors.Frame(err); ok { s += " (" + path.Base(frame.Function) + ")" } ss = append(ss, s) } return strings.Join(ss, " - ") }
Output: code=1: base code=1 (error_example_test.go:48): base (error_example_test.go:47) code=1 - base code=1 (zerrors_test.ExampleWrap) - base (zerrors_test.ExampleWrap)
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package internal contains stateful global variables internal to zerrors.
|
Package internal contains stateful global variables internal to zerrors. |
Package zmain allows setting of some global state afeecting how all zerrors are wrapped or serialised.
|
Package zmain allows setting of some global state afeecting how all zerrors are wrapped or serialised. |