errors

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 4 Imported by: 0

README

errors Unit-Test codecov

Package errors is simple error handler for Go language.
It's reference pkg/errors and privides similar functionality.

Installation

This package can be installed with the go get command:

go get github.com/ebiy0rom0/errors

Usage

This package obtains stack traces for all functions.
If want to issue your own errors(e.g. validation checks) use errors.New or errors.Errorf.

  // Cases requiring 0 or more for param
  if param <= 0 {
    return errors.New("param is less than 0.")
    // or new format error is:
    // return errors.Errorf("param is %d but required 0 or more.", param)
  }

If using an existing error or an error returned by a function, use errors.Wrap or errors.Wrapf.
Both can wrap variables that satisfy the error interface.

  // Cases failed file open and returned os.ErrNotExist
  name := "example.log"
  fp, err := os.Open(name)
  if err != nil {
    return errors.Wrap(err, "no log output distination.")
    // or wrap format error is:
    // return errors.Wrapf(err, "no log file %s", name)
  }

If you don't want to add a message to the error to wrap, you can use errors.WithStack.
This is useful when the wrap message to be wrapped is equivalent to an error message.

  // Cases failed check to file exists and returned os.ErrNotExist
  name := "example.txt"
  info, err := os.Stat(name)
  if err != nil {
    // The wrap message notifies that the file does not exist,
    // but it's satisfied by os.ErrNotExist.
    return errors.WithStack(err)
  }

errors.Wrap and errors.Wrapf implements Unwrap and can be checked with standard errors.Is and errors.As.
Equivalent functions are provided by this package and can be used.

  // fileopen() returns os.ErrNotExist wrapped by errors.Wrap
  err := fileopen(name)
  if errors.Is(err, os.ErrNotExist) {
    fmt.Printf("error is %v", os.ErrNotExist)
  }

Formatted printing of errors

This package implement fmt.Formatter and supports %s and %v by the fmt package.

%s  print the error. If for wrapped error, 
    print at chained recursively.
%v  simultaneously printed stack trace obtained at oldest.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.2.0

func As(err error, target any) bool

As the wrapper for the standard errors.As(). It's returns same result.

func Errorf

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

Errorf returns the error interface that added stack trace. Standard formatting can be used for error message.

func Is added in v0.2.0

func Is(err, target error) bool

Is the wrapper for the standard errors.Is(). It's returns same result.

func New

func New(msg string) error

New returns the error interface that added stack trace. Specify any string for the error message.

func WithStack added in v0.3.0

func WithStack(err error) error

WithStack returns the error interface that wrapped other error and added stack trace. If err is nil returns nil.

func Wrap

func Wrap(err error, msg string) error

Wrap returns the error interface that wrapped args error and added stack trace. If err is nil returns unwrapped error(=fundamental).

func Wrapf

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

Wrapf returns the error interface that wrapped args error and added stack trace. Standard formatting can be used for error message. If err is nil returns unwrapped error(=fundamental).

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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