Documentation
¶
Overview ¶
Package errors provides functions and types for effective work with errors, the wraps, and stack traces.
Index ¶
- func As(err error, target interface{}) bool
- func Is(err, target error) bool
- func JSONStack(err error) json.RawMessage
- func New(msg string) error
- func NewWithFields(msg string, fields map[string]interface{}) error
- func Restore(stack []map[string]interface{}) error
- func RestoreRaw(stack []byte) error
- func Stack(err error) []map[string]interface{}
- func Unwrap(err error) error
- func Wrap(err error, msg string) error
- func WrapWithFields(err error, msg string, fields map[string]interface{}) error
- type Error
- type Fields
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
func JSONStack ¶
func JSONStack(err error) json.RawMessage
JSONStack returns a json-encoded stack of errors.
func NewWithFields ¶
NewWithFields creates new error with contextual fields.
func RestoreRaw ¶
RestoreRaw restores an error from a result returned by JSONStack func.
Example ¶
package main
import (
"fmt"
"github.com/krocos/errors"
)
func main() {
err := errors.NewWithFields("1", errors.Fields{
"f1": "v1",
})
err = errors.WrapWithFields(err, "2", errors.Fields{
"f2": "v2",
})
b := errors.JSONStack(err)
fmt.Println(string(b))
// Restore error from a []byte (json) stack.
err = errors.RestoreRaw(b)
if err != nil {
fmt.Println(err.Error())
}
}
Output: [{"fields":{"f2":"v2"},"message":"2"},{"fields":{"f1":"v1"},"message":"1"}] 2: 1
func Unwrap ¶
Unwrap returns previous error if err's type contains an Unwrap method returning error, otherwise, Unwrap returns nil. (Doc copied from builtin function.)
Types ¶
Click to show internal directories.
Click to hide internal directories.