Documentation ¶
Overview ¶
Package xerror implements helpers for errors
Index ¶
- func TryUnwrap[T any](val T, err error) T
- type Stack
- type StackTrace
- type XError
- func (e *XError) Error() string
- func (e *XError) Format(s fmt.State, verb rune)
- func (e *XError) Id(id string) *XError
- func (e *XError) Info() *errInfo
- func (e *XError) Is(target error) bool
- func (e *XError) StackTrace() StackTrace
- func (e *XError) Stacks() []*Stack
- func (e *XError) Unwrap() error
- func (e *XError) Values() map[string]any
- func (e *XError) With(key string, value any) *XError
- func (e *XError) Wrap(cause error) *XError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TryUnwrap ¶ added in v2.1.15
TryUnwrap if err is nil then it returns a valid value If err is not nil, Unwrap panics with err. Play: https://go.dev/play/p/w84d7Mb3Afk
Example ¶
result1 := TryUnwrap(strconv.Atoi("42")) fmt.Println(result1) _, err := strconv.Atoi("4o2") defer func() { v := recover() result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error()) fmt.Println(result2) }() TryUnwrap(strconv.Atoi("4o2"))
Output: 42 true
Types ¶
type Stack ¶ added in v2.1.15
Stack contains function, file and line number info in the stack trace.
type StackTrace ¶ added in v2.1.15
type StackTrace []frame
StackTrace is array of frame. It's exported for compatibility with github.com/pkg/errors
func (StackTrace) Format ¶ added in v2.1.15
func (st StackTrace) Format(s fmt.State, verb rune)
Format formats the stack of Frames according to the fmt.Formatter interface.
%s lists source files for each Frame in the stack %v lists the source file and line number for each Frame in the stack
Format accepts flags that alter the printing of some verbs, as follows:
%+v Prints filename, function, and line number for each Frame in the stack.
type XError ¶ added in v2.1.15
type XError struct {
// contains filtered or unexported fields
}
XError is to handle error related information.
func New ¶ added in v2.1.15
New creates a new XError with message
Example ¶
err := New("error") fmt.Println(err.Error())
Output: error
func Wrap ¶ added in v2.1.15
Wrap creates a new XError and add message.
Example ¶
err := New("wrong password") wrapErr := Wrap(err, "error") fmt.Println(wrapErr.Error())
Output: error: wrong password
func (*XError) Format ¶ added in v2.1.15
Format returns: - %v, %s, %q: formatted message - %+v: formatted message with stack trace
func (*XError) Id ¶ added in v2.1.15
Id sets id to check equality in XError.Is
Example ¶
err1 := New("error").Id("e001") err2 := New("error").Id("e001") err3 := New("error").Id("e003") equal := err1.Is(err2) notEqual := err1.Is(err3) fmt.Println(equal) fmt.Println(notEqual)
Output: true false
func (*XError) Info ¶ added in v2.1.15
func (e *XError) Info() *errInfo
Info returns information of xerror, which can be printed.
Example ¶
cause := errors.New("error") err := Wrap(cause, "invalid username").Id("e001").With("level", "high") errInfo := err.Info() fmt.Println(errInfo.Id) fmt.Println(errInfo.Cause) fmt.Println(errInfo.Values["level"]) fmt.Println(errInfo.Message)
Output: e001 error high invalid username
func (*XError) Is ¶ added in v2.1.15
Is checks if target error is XError and Error.id of two errors are matched.
Example ¶
err1 := New("error").Id("e001") err2 := New("error").Id("e001") err3 := New("error").Id("e003") equal := err1.Is(err2) notEqual := err1.Is(err3) fmt.Println(equal) fmt.Println(notEqual)
Output: true false
func (*XError) StackTrace ¶ added in v2.1.15
func (e *XError) StackTrace() StackTrace
StackTrace returns stack trace which is compatible with pkg/errors
Example ¶
err := New("error") stacks := err.Stacks() fmt.Println(stacks[0].Func) fmt.Println(stacks[0].Line) containFile := strings.Contains(stacks[0].File, "xerror_example_test.go") fmt.Println(containFile)
Output: github.com/duke-git/lancet/v2/xerror.ExampleXError_StackTrace 52 true
func (*XError) Unwrap ¶ added in v2.1.15
Unwrap compatible with github.com/pkg/errors
Example ¶
err1 := New("error").With("level", "high") err2 := err1.Wrap(errors.New("invalid username")) err := err2.Unwrap() fmt.Println(err.Error())
Output: invalid username
func (*XError) Values ¶ added in v2.1.15
Values returns map of key and value that is set by With. All wrapped xerror.XError key and values will be merged. Key and values of wrapped error is overwritten by upper xerror.XError.
Example ¶
err := New("error").With("level", "high") errLevel := err.Values()["level"] fmt.Println(errLevel)
Output: high
func (*XError) With ¶ added in v2.1.15
With adds key and value related to the error object
Example ¶
err := New("error").With("level", "high") errLevel := err.Values()["level"] fmt.Println(errLevel)
Output: high