errx

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 9 Imported by: 4

README

errx

errx is a more modern and convenient error handling library, providing more user-friendly stacktrace:

Quick Start

err := errx.New("oops: something wrong :(")
err = errx.Wrap(err, "failed to do something")
fmt.Printf("%+v\n", err)

v3

v3 supports Error Code

Install
go get github.com/xTransact/errx/v3
Example
err := errx.WithCode(errcode.InternalServerError).New("failed to do something")
err = Wrap(err, "oops: :(")
err = Wrapf(err, "xxxxxxx")
fmt.Printf("%+v\n", err)

Result:

500: Internal Server Error: xxxxxxx: oops: :(: failed to do something
  Thrown: failed to do something
    --- at /home/xbank/go/pkg/errx/errx_test.go:17 TestCode1()
  Thrown: oops: :(
    --- at /home/xbank/go/pkg/errx/errx_test.go:18 TestCode1()
  Thrown: xxxxxxx
    --- at /home/xbank/go/pkg/errx/errx_test.go:19 TestCode1()

v2

v2 provides cleaner stacktrace.

Install
go get github.com/xTransact/errx/v2
Example
func a() error {
	return errx.Wrap(b(), "a()")
}

func b() error {
	return c()
}

func c() error {
	return d()
}

func d() error {
	return e()
}

func e() error {
	return f()
}

func f() error {
	return errx.Wrap(g(), "f()")
}

func g() error {
	return fmt.Errorf("nil pointer dereference")
}

func main() {
    err := a()
    err = Wrapf(err, "internal server error: %s", "xBank")
    fmt.Printf("%+v\n", err)
}

Result:

internal server error: xBank: a(): f(): nil pointer dereference
  Thrown: f()
    --- at /home/xbank/go/pkg/errx/errx_test.go:47 f()
  Thrown: a()
    --- at /home/xbank/go/pkg/errx/errx_test.go:27 a()
  Thrown: internal server error: xBank
    --- at /home/xbank/go/pkg/errx/errx_test.go:56 main()

v1

Install

go get github.com/xTransact/errx

Example

TestErrxWrap Failed: oops user: xBank: failed to check: xxx: something wrong.
Stacktrace:
  Error: something wrong.
    --- at /home/xbank/go/pkg/errx/errx_test.go:29 f()
    --- at /home/xbank/go/pkg/errx/errx_test.go:25 e()
    --- at /home/xbank/go/pkg/errx/errx_test.go:21 d()
    --- at /home/xbank/go/pkg/errx/errx_test.go:17 c()
    --- at /home/xbank/go/pkg/errx/errx_test.go:13 b()
  Thrown: xxx
    --- at /home/xbank/go/pkg/errx/errx_test.go:9 a()
    --- at /home/xbank/go/pkg/errx/errx_test.go:33 TestErrxWrap()
  Thrown: failed to check
    --- at /home/xbank/go/pkg/errx/errx_test.go:34 TestErrxWrap()
  Thrown: oops user: xBank
    --- at /home/xbank/go/pkg/errx/errx_test.go:35 TestErrxWrap()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
    Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func Errorf

func Errorf(format string, args ...any) error

Errorf formats an error and returns `errx.Error` object that satisfies `error`.

func GetChainedMessage

func GetChainedMessage(err error) string

func GetCode

func GetCode(err error) errcode.Code

func GetMessage

func GetMessage(err error) (bool, string)

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func New

func New(message string) error

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

func WithStack

func WithStack(err error) error

WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap wraps an error into an `errx.Error` object that satisfies `error`

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf wraps an error into an `errx.Error` object that satisfies `error` and formats an error message.

Types

type Builder

type Builder xerr

func NewBadRequestError

func NewBadRequestError() Builder

func NewForbiddenError

func NewForbiddenError() Builder

func NewInternalServerError

func NewInternalServerError() Builder

func NewNotFoundError

func NewNotFoundError() Builder

func NewUnauthorizedError

func NewUnauthorizedError() Builder

func WithCode

func WithCode(code errcode.Code) Builder

func (Builder) Build

func (b Builder) Build() error

func (Builder) Errorf

func (b Builder) Errorf(format string, args ...any) error

func (Builder) New

func (b Builder) New(message string) error

func (Builder) WithStack

func (b Builder) WithStack(err error) error

func (Builder) Wrap

func (b Builder) Wrap(err error, message string) error

func (Builder) Wrapf

func (b Builder) Wrapf(err error, format string, args ...any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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