errorx

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 8 Imported by: 35

README

ErrorX

errorx provide an enhanced error implements for go, allow with stacktraces and wrap another error.

Install

go get github.com/gookit/goutil/errorx

Go docs

Usage

Create error with call stack info
  • use the errorx.New instead errors.New
func doSomething() error {
    if false {
	    // return errors.New("a error happen")
	    return errorx.New("a error happen")
	}
}
  • use the errorx.Newf or errorx.Errorf instead fmt.Errorf
func doSomething() error {
    if false {
	    // return fmt.Errorf("a error %s", "happen")
	    return errorx.Newf("a error %s", "happen")
	}
}
Wrap the previous error

used like this before:

    if err := SomeFunc(); err != nil {
	    return err
	}

can be replaced with:

    if err := SomeFunc(); err != nil {
	    return errors.Stacked(err)
	}

Output details

error output details for use errorx

Use errorx.New

errorx functions for new error:

func New(msg string) error
func Newf(tpl string, vars ...interface{}) error
func Errorf(tpl string, vars ...interface{}) error

Examples:

    err := errorx.New("the error message")

    fmt.Println(err)
    // fmt.Printf("%v\n", err)
    // fmt.Printf("%#v\n", err)

from the test: errorx_test.TestNew()

Output:

the error message
STACK:
github.com/gookit/goutil/errorx_test.returnXErr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:21
github.com/gookit/goutil/errorx_test.returnXErrL2()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:25
github.com/gookit/goutil/errorx_test.TestNew()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:29
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571
Use errorx.With

errorx functions for with another error:

func With(err error, msg string) error
func Withf(err error, tpl string, vars ...interface{}) error

With a go raw error:

	err1 := returnErr("first error message")

	err2 := errorx.With(err1, "second error message")
	fmt.Println(err2)

from the test: errorx_test.TestWith_goerr()

Output:

second error message
STACK:
github.com/gookit/goutil/errorx_test.TestWith_goerr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:51
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Previous: first error message

With a errorx error:

	err1 := returnXErr("first error message")
	err2 := errorx.With(err1, "second error message")
	fmt.Println(err2)

from the test: errorx_test.TestWith_errorx()

Output:

second error message
STACK:
github.com/gookit/goutil/errorx_test.TestWith_errorx()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:64
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Previous: first error message
STACK:
github.com/gookit/goutil/errorx_test.returnXErr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:21
github.com/gookit/goutil/errorx_test.TestWith_errorx()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:61
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Use errorx.Wrap
err := errors.New("first error message")
err = errorx.Wrap(err, "second error message")
err = errorx.Wrap(err, "third error message")
// fmt.Println(err)
// fmt.Println(err.Error())

Direct print the err:

third error message
Previous: second error message
Previous: first error message

Print the err.Error():

third error message; second error message; first error message

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./errorx/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./errorx/...

Refers

Documentation

Overview

Package errorx provide an enhanced error implements for go, allow with stacktraces and wrap another error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As same of the To(), alias of errors.As()

NOTICE: target must be ptr and not nil

func Cause

func Cause(err error) error

Cause returns the first cause error by call err.Cause(). Otherwise, will returns current error.

func Config

func Config(fns ...func(opt *ErrStackOpt))

Config the stdOpt setting

func Errorf

func Errorf(tpl string, vars ...any) error

Errorf error with format message, and with caller stacks

func Has

func Has(err, target error) bool

Has check err has contains target, or err is eq target. alias of errors.Is()

func Is

func Is(err, target error) bool

Is alias of errors.Is()

func New

func New(msg string) error

New error message and with caller stacks

func Newf

func Newf(tpl string, vars ...any) error

Newf error with format message, and with caller stacks. alias of Errorf()

func Previous

func Previous(err error) error

Previous alias of Unwrap()

func Raw

func Raw(msg string) error

Raw new a raw go error. alias of errors.New()

func Rawf added in v0.5.1

func Rawf(tpl string, vars ...any) error

Rawf new a raw go error. alias of errors.New()

func ResetStdOpt added in v0.5.5

func ResetStdOpt()

ResetStdOpt config

func SkipDepth

func SkipDepth(skipDepth int) func(opt *ErrStackOpt)

SkipDepth setting

func Stacked

func Stacked(err error) error

Stacked warp a go error and with caller stacks. alias of WithStack()

func To

func To(err error, target any) bool

To try convert err to target, returns is result.

NOTICE: target must be ptr and not nil. alias of errors.As()

Usage:

var ex *errorx.ErrorX
err := doSomething()
if errorx.To(err, &ex) {
	fmt.Println(ex.GoString())
}

func TraceDepth

func TraceDepth(traceDepth int) func(opt *ErrStackOpt)

TraceDepth setting

func Traced

func Traced(err error) error

Traced warp a go error and with caller stacks. alias of WithStack()

func Unwrap

func Unwrap(err error) error

Unwrap returns previous error by call err.Unwrap(). Otherwise, will returns nil.

func With

func With(err error, msg string) error

With prev error and error message, and with caller stacks

func WithOptions

func WithOptions(msg string, fns ...func(opt *ErrStackOpt)) error

WithOptions new error with some option func

func WithPrev

func WithPrev(err error, msg string) error

WithPrev error and message, and with caller stacks. alias of With()

func WithPrevf

func WithPrevf(err error, tpl string, vars ...any) error

WithPrevf error and with format message, and with caller stacks. alias of Withf()

func WithStack

func WithStack(err error) error

WithStack wrap a go error with a stacked trace. If err is nil, will return nil.

func Withf

func Withf(err error, tpl string, vars ...any) error

Withf error and with format message, and with caller stacks

func Wrap

func Wrap(err error, msg string) error

Wrap error and with message, but not with stack

func Wrapf

func Wrapf(err error, tpl string, vars ...any) error

Wrapf error with format message, but not with stack

Types

type Causer

type Causer interface {
	// Cause returns the first cause error by call err.Cause().
	// Otherwise, will returns current error.
	Cause() error
}

Causer interface for get first cause error

type ErrList added in v0.6.0

type ErrList = Errors

type ErrMap added in v0.6.0

type ErrMap map[string]error

ErrMap multi error map

func (ErrMap) Error added in v0.6.0

func (e ErrMap) Error() string

Error string

func (ErrMap) ErrorOrNil added in v0.6.0

func (e ErrMap) ErrorOrNil() error

ErrorOrNil error

func (ErrMap) IsEmpty added in v0.6.0

func (e ErrMap) IsEmpty() bool

IsEmpty error

func (ErrMap) One added in v0.6.0

func (e ErrMap) One() error

One error

type ErrStackOpt

type ErrStackOpt struct {
	SkipDepth  int
	TraceDepth int
}

ErrStackOpt struct

type ErrorCoder

type ErrorCoder interface {
	error
	Code() int
}

ErrorCoder interface

type ErrorR

type ErrorR interface {
	ErrorCoder
	fmt.Stringer
	IsSuc() bool
	IsFail() bool
}

ErrorR useful for web service replay/response. code == 0 is successful. otherwise, is failed.

func Fail

func Fail(code int, msg string) ErrorR

Fail code with error response

func NewR

func NewR(code int, msg string) ErrorR

NewR code with error response

func Suc

func Suc(msg string) ErrorR

Suc success response reply

type ErrorX

type ErrorX struct {
	// contains filtered or unexported fields
}

ErrorX struct

TIPS:

fmt pkg call order: Format > GoString > Error > String

func ToErrorX added in v0.5.5

func ToErrorX(err error) (ex *ErrorX, ok bool)

ToErrorX convert check

func (*ErrorX) CallerFunc

func (e *ErrorX) CallerFunc() *Func

CallerFunc returns the error caller func. if stack is nil, will return nil

func (ErrorX) CallerPC

func (s ErrorX) CallerPC() uintptr

CallerPC the caller PC value in the stack. it is first frame.

func (*ErrorX) Cause

func (e *ErrorX) Cause() error

Cause implements Causer.

func (*ErrorX) Error

func (e *ErrorX) Error() string

Error to string, not contains stack information.

func (*ErrorX) Format

func (e *ErrorX) Format(s fmt.State, verb rune)

Format error

func (*ErrorX) GoString

func (e *ErrorX) GoString() string

GoString to GO string, contains stack information. printing an error with %#v will produce useful information.

func (*ErrorX) Location

func (e *ErrorX) Location() string

Location information for the caller func. more please see CallerFunc

Returns eg:

github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34

func (*ErrorX) Message

func (e *ErrorX) Message() string

Message error message of current

func (ErrorX) StackFrames

func (s ErrorX) StackFrames() *runtime.Frames

StackFrames stack frame list

func (ErrorX) StackLen

func (s ErrorX) StackLen() int

StackLen for error

func (*ErrorX) StackString

func (e *ErrorX) StackString() string

StackString returns error stack string of current.

func (*ErrorX) String

func (e *ErrorX) String() string

String error to string, contains stack information.

func (*ErrorX) Unwrap

func (e *ErrorX) Unwrap() error

Unwrap implements Unwrapper.

func (*ErrorX) WriteTo

func (e *ErrorX) WriteTo(w io.Writer) (n int64, err error)

WriteTo write the error to a writer, contains stack information.

type Errors added in v0.6.0

type Errors []error

Errors multi error list

func (Errors) Error added in v0.6.0

func (es Errors) Error() string

Error string

func (Errors) ErrorOrNil added in v0.6.0

func (es Errors) ErrorOrNil() error

ErrorOrNil error

func (Errors) First added in v0.6.0

func (es Errors) First() error

First error

func (Errors) IsEmpty added in v0.6.0

func (es Errors) IsEmpty() bool

IsEmpty error

type Func

type Func struct {
	*runtime.Func
	// contains filtered or unexported fields
}

Func struct

func FuncForPC

func FuncForPC(pc uintptr) *Func

FuncForPC create.

func (*Func) FileLine

func (f *Func) FileLine() (file string, line int)

FileLine of the func

func (*Func) Location

func (f *Func) Location() string

Location simple location info for the func

Returns eg:

github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34

func (*Func) MarshalText

func (f *Func) MarshalText() ([]byte, error)

MarshalText handle

func (*Func) String

func (f *Func) String() string

String of the func

Returns eg:

github.com/gookit/goutil/errorx_test.TestWithPrev()
  At /path/to/github.com/gookit/goutil/errorx_test.go:34

type Unwrapper

type Unwrapper interface {
	// Unwrap returns previous error by call err.Unwrap().
	// Otherwise, will returns nil.
	Unwrap() error
}

Unwrapper interface for get previous error

type XErrorFace

type XErrorFace interface {
	error
	Causer
	Unwrapper
}

XErrorFace interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL