Documentation ¶
Overview ¶
Package errs implements functions to manipulate error instances.
Index ¶
- func As(err error, target interface{}) bool
- func Cause(err error) errordeprecated
- func EncodeJSON(err error) string
- func Is(err, target error) bool
- func Join(errlist ...error) error
- func New(msg string, opts ...ErrorContextFunc) error
- func Unwrap(err error) error
- func Unwraps(err error) []error
- func Wrap(err error, opts ...ErrorContextFunc) error
- type Error
- func (e *Error) EncodeJSON() string
- 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) MarshalJSON() ([]byte, error)
- func (e *Error) SetCause(err error) *Error
- func (e *Error) SetContext(name string, value interface{}) *Error
- func (e *Error) String() string
- func (e *Error) Unwrap() error
- type ErrorContextFunc
- type Errors
- func (es *Errors) Add(errlist ...error)
- func (es *Errors) EncodeJSON() string
- func (es *Errors) Error() string
- func (es *Errors) ErrorOrNil() error
- func (es *Errors) Format(s fmt.State, verb rune)
- func (es *Errors) GoString() string
- func (es *Errors) MarshalJSON() ([]byte, error)
- func (es *Errors) String() string
- func (es *Errors) Unwrap() []error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeJSON ¶
EncodeJSON function dumps out error instance with JSON format.
Example ¶
package main import ( "fmt" "os" "github.com/goark/errs" ) func main() { _, err := os.Open("not-exist.txt") fmt.Printf("%v", errs.EncodeJSON(err)) }
Output: {"Type":"*fs.PathError","Msg":"open not-exist.txt: no such file or directory","Cause":{"Type":"syscall.Errno","Msg":"no such file or directory"}}
func Join ¶ added in v1.3.0
Join function returns Errors instance.
Example ¶
package main import ( "errors" "fmt" "github.com/goark/errs" ) func main() { err := errs.Join(errors.New("error 1"), errors.New("error 2")) fmt.Println(err) errlist, ok := err.(*errs.Errors) if !ok { return } errlist.Add(errors.New("error 3")) fmt.Printf("%+v\n", errlist.ErrorOrNil()) }
Output: error 1 error 2 {"Type":"*errs.Errors","Errs":[{"Type":"*errors.errorString","Msg":"error 1"},{"Type":"*errors.errorString","Msg":"error 2"},{"Type":"*errors.errorString","Msg":"error 3"}]}
func New ¶
func New(msg string, opts ...ErrorContextFunc) error
New function returns an error instance with message and context informations.
Example ¶
package main import ( "fmt" "os" "github.com/goark/errs" ) func main() { err := errs.New( "wrapper error", errs.WithCause(os.ErrInvalid), errs.WithContext("foo", "bar"), ) fmt.Printf("%+v", err) }
Output: {"Type":"*errs.Error","Err":{"Type":"*errors.errorString","Msg":"wrapper error"},"Context":{"foo":"bar","function":"github.com/goark/errs_test.ExampleNew"},"Cause":{"Type":"*errors.errorString","Msg":"invalid argument"}}
func Unwraps ¶ added in v1.2.0
Unwraps function finds cause errors ([]error slice) in target error instance.
func Wrap ¶
func Wrap(err error, opts ...ErrorContextFunc) error
Wrap function returns a wrapping error instance with context informations.
Types ¶
type Error ¶
type Error struct { Err error Cause error Context map[string]interface{} // contains filtered or unexported fields }
Error type is a implementation of error interface. This type is for wrapping cause error instance.
Example ¶
package main import ( "fmt" "os" "github.com/goark/errs" ) func main() { err := errs.Wrap( os.ErrInvalid, errs.WithContext("foo1", "bar1"), ) _ = err.(*errs.Error).SetContext("foo2", "bar2") fmt.Printf("%+v", err) }
Output: {"Type":"*errs.Error","Err":{"Type":"*errors.errorString","Msg":"invalid argument"},"Context":{"foo1":"bar1","foo2":"bar2","function":"github.com/goark/errs_test.ExampleError"}}
func (*Error) EncodeJSON ¶
EncodeJSON method returns serialize string of Error with JSON format.
func (*Error) Error ¶
Error method returns error message. This method is a implementation of error interface.
func (*Error) Format ¶
Format method returns formatted string of Error instance. This method is a implementation of fmt.Formatter interface.
func (*Error) GoString ¶
GoString method returns serialize string of Error. This method is a implementation of fmt.GoStringer interface.
func (*Error) Is ¶
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) MarshalJSON ¶
MarshalJSON method returns serialize string of Error with JSON format. This method is implementation of json.Marshaler interface.
func (*Error) SetContext ¶
SetContext method sets context information
type ErrorContextFunc ¶
type ErrorContextFunc func(*Error)
ErrorContextFunc type is self-referential function type for New and Wrap functions. (functional options pattern)
func WithCause ¶
func WithCause(err error) ErrorContextFunc
WithCause function returns ErrorContextFunc function value. This function is used in New and Wrap functions that represents context (key/value) data.
func WithContext ¶
func WithContext(name string, value interface{}) ErrorContextFunc
WithContext function returns ErrorContextFunc function value. This function is used in New and Wrap functions that represents context (key/value) data.
type Errors ¶ added in v1.3.0
type Errors struct {
// contains filtered or unexported fields
}
Errors is multiple error instance.
Example ¶
package main import ( "fmt" "sync" "github.com/goark/errs" ) func main() { errlist := &errs.Errors{} var wg sync.WaitGroup for i := 1; i <= 100000; i++ { i := i wg.Add(1) go func() { defer wg.Done() errlist.Add(fmt.Errorf("error %d", i)) }() } wg.Wait() fmt.Println("error ount =", len(errlist.Unwrap())) }
Output: error ount = 100000
func (*Errors) EncodeJSON ¶ added in v1.3.0
EncodeJSON method returns serialize string of Errors with JSON format.
func (*Errors) Error ¶ added in v1.3.0
Error method returns error message. This method is a implementation of error interface.
func (*Errors) ErrorOrNil ¶ added in v1.3.0
ErrorOrNil method returns this as a error type.
func (*Errors) Format ¶ added in v1.3.0
Format method returns formatted string of Errors instance. This method is a implementation of fmt.Formatter interface.
func (*Errors) GoString ¶ added in v1.3.0
GoString method returns serialize string of Errors. This method is a implementation of fmt.GoStringer interface.
func (*Errors) MarshalJSON ¶ added in v1.3.0
MarshalJSON method returns serialize string of Errors with JSON format. This method is implementation of json.Marshaler interface.