errorz

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: MIT Imports: 4 Imported by: 1

README

gopherz

ezpkg.io/errorz

PkgGoDev GitHub License version

Package errorz provides functions for dealing with errors, with stacktrace, validation, and multi-errors.

Installation

go get -u ezpkg.io/errorz@v0.0.4

Examples

Must
data := errorz.Must(os.ReadFile(path))
fmt.Printf("%s", data)

errorz.MustZ(os.WriteFile(path, data, 0644))
Stacktrace
err := fmt.Errorf("foo")
zErr := errorz.Wrapf(err, "bar/%v", "one")

fmt.Printf("%v\n", zErr)
// bar/one: foo

fmt.Printf("%+v\n", zErr)
// bar/one: foo
// ezpkg.io/errorz_test.TestError.func1
// /Users/i/ws/ezpkg/ezpkg/errorz/errorz_test.go:12
// testing.tRunner
// /usr/local/go/src/testing/testing.go:7890

fmt.Printf("%#v\n", zErr)
// bar/one: foo
// ezpkg.io/errorz_test/errorz_test.go:12 · TestError.func1
// testing/testing.go:7890 · tRunner

No stacktrace:

zErr := errorz.NoStack().New("no stack")
fmt.Printf("%+v", zErr)
Multi-errors
var err error
errorz.AppendTo(&err, fmt.Errorf("foo"))
errorz.AppendTo(&err, nil)
fmt.Printf("%+v", err)

var err2 error
err2 = errorz.Append(err2, err)
fmt.Printf("%+v", err2)

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(err0 error, errs ...error) error

func AppendTo

func AppendTo(pErr *error, errs ...error)

func AppendTof

func AppendTof(pErr *error, err error, msg string, args ...any)

func Appendf

func Appendf(err0 error, err error, msg string, args ...any) error

func Error

func Error(msg string) error

func Errorf

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

func GetErrors

func GetErrors(err error) []error

func Must

func Must[T any](v T, err error) T

func MustZ

func MustZ(err error)

func New

func New(msg string) error

func Newf

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

func ValidateX

func ValidateX[T any](pErr *error, value T, condition bool, msg string, args ...any) (out T)

func Validatef

func Validatef(pErr *error, condition bool, msg string, args ...any)

func Wrap

func Wrap(err error, msg string) error

func Wrapf

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

Types

type Errors

type Errors interface {
	error
	Errors() []error
}

type Option

type Option struct {
	NoStack     bool
	CallersSkip int
}

func CallersSkip

func CallersSkip(n int) Option

func NoStack

func NoStack() Option

func (Option) Append

func (opt Option) Append(pErr *error, errs ...error)

func (Option) Appendf

func (opt Option) Appendf(pErr *error, err error, msgArgs ...any)

func (Option) Error

func (opt Option) Error(msg string) error

func (Option) Errorf

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

func (Option) New

func (opt Option) New(msg string) error

func (Option) Newf

func (opt Option) Newf(format string, args ...any) error

func (Option) Wrap

func (opt Option) Wrap(err error, msg string) error

func (Option) Wrapf

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

Jump to

Keyboard shortcuts

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