errorz

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 4 Imported by: 0

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/[email protected]

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)

Similar Packages

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 Combine added in v0.1.2

func Combine(errs ...error) (err error)

func Error

func Error(msg string) error

func Errorf

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

func GetErrors

func GetErrors(err error) []error

GetErrors unwraps the error and return the list of errors.

func MapFunc added in v0.1.0

func MapFunc[T, Q any](fn func(T) Q) func(v T, err error) (Q, error)

MapFunc maps a value and an error to another value and error using the input function.

func Must

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

func Must2 added in v0.0.7

func Must2[A, B any](a A, b B, err error) (A, B)

func Must3 added in v0.0.7

func Must3[A, B, C any](a A, b B, c C, err error) (A, B, C)

func Must4 added in v0.0.7

func Must4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D)

func Must5 added in v0.0.7

func Must5[A, B, C, D, E any](a A, b B, c C, d D, e E, err error) (A, B, C, D, E)

func Must6 added in v0.0.7

func Must6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F, err error) (A, B, C, D, E, F)

func Must7 added in v0.0.7

func Must7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G, err error) (A, B, C, D, E, F, G)

func Must8 added in v0.0.7

func Must8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H, err error) (A, B, C, D, E, F, G, H)

func MustValidate added in v0.0.8

func MustValidate(condition bool, msgArgs ...any)

func MustValidateX added in v0.0.8

func MustValidateX[T any](value T, condition bool, msgArgs ...any) T

func MustValidateXf added in v0.0.8

func MustValidateXf[T any](value T, condition bool, msg string, args ...any) T

func MustValidatef added in v0.0.8

func MustValidatef(condition bool, msg string, args ...any)

func MustZ

func MustZ(err error)

func New

func New(msg string) error

func Newf

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

func Validate added in v0.0.8

func Validate(condition bool, msgArgs ...any) error

func ValidateTo added in v0.0.8

func ValidateTo(pErr *error, condition bool, msgArgs ...any)

func ValidateToX added in v0.0.8

func ValidateToX[T any](pErr *error, value T, condition bool, msgArgs ...any) (out T)

func ValidateToXf added in v0.0.8

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

func ValidateTof added in v0.0.8

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

func ValidateX

func ValidateX[T any](value T, condition bool, msgArgs ...any) (T, error)

func ValidateXf added in v0.0.8

func ValidateXf[T any](value T, condition bool, msgArgs ...any) (T, error)

func Validatef

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

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
	Unwrap() []error
	stacktracez.StackTracerZ
}

func ToErrors added in v0.1.1

func ToErrors(err error) Errors

type Option

type Option struct {
	NoStack     bool
	CallersSkip int
}

func CallersSkip

func CallersSkip(n int) Option

func NoStack

func NoStack() Option

func (Option) AddSkip added in v0.1.1

func (opt Option) AddSkip(n int) Option

func (Option) Append

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

func (Option) AppendTo added in v0.1.1

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

func (Option) AppendTof added in v0.1.1

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

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) Validate added in v0.1.1

func (opt Option) Validate(condition bool, msgArgs ...any) error

func (Option) Validatef added in v0.1.1

func (opt Option) Validatef(condition bool, msg 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