errorx

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 7 Imported by: 0

README

Golang native error extension

Helper for customization and wrapping actual errors, hiding from users

Usage example

// wrap inner error
err := errors.New("new inner error")
err = errorx.New("user_friendly_text", errorx.WithInner(err))
...
// add caller line
err = errorx.New("user_friendly_text", errorx.WithInner(err), errorx.WithCaller())
...
// add custom fields
err = errorx.New("user_friendly_text", 
	errorx.WithInner(err), 
	errorx.WithString("key", "value"),
	errorx.WithInt("key2", 123),
)
...
// typed error
const CustomType errorx.Type = 1

err = errorx.New("user_friendly_text", errorx.WithInner(err), errorx.WithType(CustomType))
...
// extract custom fields to json
if errX := errorx.AsError(err); errX != nil {
    str, _ := errX.JSONString()
    log.Printf("error: %s fields: %s", err, str)
}
...
// extract custom fields
if errX := errorx.AsError(err); errX != nil {
    for _, field := range errX.Fields() {
		var key, value string
		key = field.Key
        value = field.Value
    }
}
...
// check custom type
switch errX.Type() {
    case CustomType:
		// handle custom type err
    default:
        // do something else
}

See full integration example here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsError

func AsError(err error) *errorx

func New

func New(text string, opts ...Option) error

func Wrap added in v1.0.1

func Wrap(err error, opts ...Option) error

Types

type Field

type Field struct {
	Key, Value string
}

type Option

type Option func(*errorx)

func WithAny

func WithAny(key string, value any) Option

func WithCaller

func WithCaller(skip ...int) Option

func WithInner deprecated

func WithInner(err error) Option

Deprecated: use WithInternal instead

func WithInt

func WithInt(key string, value int64) Option

func WithInternal added in v1.0.1

func WithInternal(err error) Option

func WithStacktrace

func WithStacktrace() Option

func WithString

func WithString(key, value string) Option

func WithType

func WithType(typ Type) Option

type Type

type Type int

Directories

Path Synopsis
example
db

Jump to

Keyboard shortcuts

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