errorx

package module
v0.0.0-...-a98703b Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 7 Imported by: 0

README

codecov

A simple error implementation for improving error handling in Golang.

  • StackTrace
  • Additional Error Data Field

Documentation

Overview

Example (CustomError_LogValue)
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
	ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
		if a.Key == "time" || a.Key == "level" {
			return slog.Attr{}
		}
		return a
	},
})))
slog.Info("test message", slog.Any("error", New("test").With("test key", "test value")))
Output:

msg="test message" error.message=test "error.test key"="test value"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool
Example
if _, err := os.Open("non-existing"); err != nil {
	var pathError *fs.PathError
	if As(err, &pathError) {
		fmt.Println("Failed at path:", pathError.Path)
	} else {
		fmt.Println(err)
	}
}
Output:

Failed at path: non-existing

func Is

func Is(err error, target error) bool
Example
if _, err := os.Open("non-existing"); err != nil {
	if Is(err, fs.ErrNotExist) {
		fmt.Println("file does not exist")
	} else {
		fmt.Println(err)
	}
}
Output:

file does not exist

func Join

func Join(errs ...error) customErrors

func Unwrap

func Unwrap(err error) error
Example
err1 := New("error1")
err2 := fmt.Errorf("error2: [%w]", err1)
fmt.Println(err2)
fmt.Println(Unwrap(err2))
// Output
// error2: [error1]
// error1
Output:

Types

type CustomError

type CustomError interface {
	error
	With(key string, value interface{}) CustomError
	WithData(map[string]any) CustomError
	Cause() error
	Unwrap() error
}

func New

func New(message string) CustomError

func Wrap

func Wrap(err error) CustomError

func WrapDepth

func WrapDepth(err error, depth int) CustomError

func WrapWithData

func WrapWithData(err error, data map[string]any) CustomError

type CustomErrors

type CustomErrors interface {
	error
	Is(error) bool
	Unwrap() []error
}

Jump to

Keyboard shortcuts

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