Documentation
¶
Overview ¶
Package errs implements functions to manipulate error instances.
Index ¶
- func Cause(err error) error
- func EncodeJSON(err error) string
- func New(msg string, opts ...ErrorContextFunc) error
- func Wrap(err error, msg string, opts ...ErrorContextFunc) error
- type Error
- func (e *Error) Error() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) GoString() string
- func (e *Error) Is(target error) bool
- func (e *Error) JSON() string
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) SetContext(name string, value interface{})
- func (e *Error) String() string
- func (e *Error) Unwrap() error
- type ErrorContextFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cause ¶
Cause function finds cause error in target error instance.
Example ¶
package main import ( "fmt" "os" "github.com/spiegel-im-spiegel/errs" ) func main() { _, err := os.Open("not-exist.txt") fmt.Printf("%v", errs.Cause(err)) }
Output: no such file or directory
func EncodeJSON ¶ added in v0.2.0
EncodeJSON function dumps out error instance with JSON format.
Example ¶
package main import ( "fmt" "os" "github.com/spiegel-im-spiegel/errs" ) func main() { _, err := os.Open("not-exist.txt") fmt.Printf("%v", errs.EncodeJSON(err)) }
Output: {"Type":"*os.PathError","Msg":"open not-exist.txt: no such file or directory","Cause":{"Type":"syscall.Errno","Msg":"no such file or directory"}}
func New ¶ added in v0.2.0
func New(msg string, opts ...ErrorContextFunc) error
New function returns an error instance with message and context informations.
Example ¶
package main import ( "fmt" "github.com/spiegel-im-spiegel/errs" ) func main() { fmt.Printf("%+v", errs.New("custom error")) }
Output: {"Type":"*errs.Error","Msg":"custom error","Context":{"function":"github.com/spiegel-im-spiegel/errs_test.ExampleNew"}}
func Wrap ¶
func Wrap(err error, msg string, opts ...ErrorContextFunc) error
Wrap function returns a wrapping error instance with message and context informations.
Example ¶
package main import ( "fmt" "os" "github.com/spiegel-im-spiegel/errs" ) func main() { fmt.Printf("%+v", errs.Wrap(os.ErrInvalid, "wrapper error")) }
Output: {"Type":"*errs.Error","Msg":"wrapper error: invalid argument","Context":{"function":"github.com/spiegel-im-spiegel/errs_test.ExampleWrap"},"Cause":{"Type":"*errors.errorString","Msg":"invalid argument"}}
Types ¶
type Error ¶ added in v0.2.0
type Error struct { //Msg element is error message. Msg string Cause error Context map[string]interface{} }
Error type is a implementation of error interface. This type is for wrapping cause error instance.
Example ¶
package main import ( "fmt" "os" "github.com/spiegel-im-spiegel/errs" ) func main() { err := errs.Wrap( os.ErrInvalid, "wrapper error", errs.WithContext("foo1", "bar1"), ) err.(*errs.Error).SetContext("foo2", "bar2") fmt.Printf("%+v", err) }
Output: {"Type":"*errs.Error","Msg":"wrapper error: invalid argument","Context":{"foo1":"bar1","foo2":"bar2","function":"github.com/spiegel-im-spiegel/errs_test.ExampleError"},"Cause":{"Type":"*errors.errorString","Msg":"invalid argument"}}
func (*Error) Error ¶ added in v0.2.0
Error method returns error message. This method is a implementation of error interface.
func (*Error) Format ¶ added in v0.2.0
Format method returns formatted string of Error instance. This method is a implementation of fmt.Formatter interface.
func (*Error) GoString ¶ added in v0.2.0
GoString method returns serialize string of Error. This method is a implementation of fmt.GoStringer interface.
func (*Error) Is ¶ added in v0.2.0
Is method reports whether any error in error's chain matches cause of target error. This method is used in errors.Is function.
func (*Error) JSON ¶ added in v0.2.0
JSON method returns serialize string of Error with JSON format.
func (*Error) MarshalJSON ¶ added in v0.3.0
MarshalJSON method returns serialize string of Error with JSON format. This method is implementation of json.Marshaler interface.
func (*Error) SetContext ¶ added in v0.3.0
SetContext method sets context information in Error instance
type ErrorContextFunc ¶ added in v0.3.0
type ErrorContextFunc func(*Error)
ErrorContextFunc type is self-referential function type for New and Wrap functions. (functional options pattern)
func WithContext ¶ added in v0.3.0
func WithContext(name, value string) ErrorContextFunc
WithContext function returns ErrorContextFunc function value. This function is used in New and Wrap functions that represents context (key/value) data.
Example ¶
package main import ( "fmt" "os" "github.com/spiegel-im-spiegel/errs" ) func main() { err := errs.Wrap( os.ErrInvalid, "wrapper error", errs.WithContext("foo", "bar"), ) fmt.Printf("%+v", err) }
Output: {"Type":"*errs.Error","Msg":"wrapper error: invalid argument","Context":{"foo":"bar","function":"github.com/spiegel-im-spiegel/errs_test.ExampleWithContext"},"Cause":{"Type":"*errors.errorString","Msg":"invalid argument"}}