errors

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 7 Imported by: 15

README

errors

A library with sane error processing primitives

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As errors.As wrapper

func Dig added in v0.2.0

func Dig[T error](err error) *T

Dig looks for error of type T in err's chain. Returns a point to an error found or nil otherwise.

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	err := fmt.Errorf("example: %w", errors.New("error"))
	if pe := errors.Dig[errors.Error](err); pe != nil {
		fmt.Println((*pe).Error())
	}

	err = fmt.Errorf("example: %w", errors.Const("const error"))
	if pe := errors.Dig[errors.Error](err); pe != nil {
		fmt.Println((*pe).Error())
	}
}
Output:

error

func Is

func Is(err, target error) bool

Is errors.Is wrapper

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	err := errors.Wrap(io.EOF, "read file")
	if !errors.Is(err, io.EOF) {
		fmt.Println("must not be here")
	}

	e := errors.New("i am an error")
	if !errors.Is(errors.Wrap(e, "covered"), e) {
		fmt.Println("must not be here again")
	}
}
Output:

func Join added in v0.6.0

func Join(errs ...error) error

Join simply wraps stdlib errors.Join.

Types

type Const added in v0.2.0

type Const string

Const an implementation of error that can be a constant

func (Const) As added in v0.2.0

func (c Const) As(target interface{}) bool

As for errors.As

func (Const) Error added in v0.2.0

func (c Const) Error() string

func (Const) Is added in v0.2.0

func (c Const) Is(err error) bool

Is for errors.Is

type Error added in v0.2.0

type Error struct {
	// contains filtered or unexported fields
}

Error implementation of error with structured context

func Just added in v0.5.0

func Just(err error) Error

Just a simple wrapper over an error that allows to add structured context values without adding any text info.

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	err := errors.Just(io.EOF).Str("name", "value")
	d := errors.GetContextDeliverer(err)
	var cons testContextConsumer
	d.Deliver(cons)
	fmt.Println(err.Error() == io.EOF.Error())

	err = errors.Just(errors.New("error").Int("value", 1))
	d = errors.GetContextDeliverer(err)
	cons = testContextConsumer{}
	d.Deliver(cons)
	fmt.Println(err.Error())

}

type testContextConsumer struct{}

func (t testContextConsumer) Bool(name string, value bool) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int(name string, value int) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int8(name string, value int8) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int16(name string, value int16) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int32(name string, value int32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int64(name string, value int64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint(name string, value uint) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint8(name string, value uint8) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint16(name string, value uint16) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint32(name string, value uint32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint64(name string, value uint64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Float32(name string, value float32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Float64(name string, value float64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) String(name string, value string) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Any(name string, value interface{}) {
	fmt.Printf("%s: %v\n", name, value)
}
Output:

name: value
true
value: 1
error

func New

func New(msg string) Error

New creates new Error with a given message

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.New("error example"))
}
Output:

error example

func Newf

func Newf(format string, a ...interface{}) Error

Newf same as New, with formatted error message

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.Newf("error %s", "example"))
}
Output:

error example

func Wrap

func Wrap(err error, msg string) Error

Wrap constructs a new error by wrapping given message over the existing one

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.Wrap(errors.Const("example"), "error"))
}
Output:

error: example

func Wrapf

func Wrapf(err error, format string, a ...interface{}) Error

Wrapf calls Wrap function with a message built using given format

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.Wrapf(errors.Const("example"), "formatted error"))
}
Output:

formatted error: example

func (Error) Any added in v0.2.0

func (e Error) Any(name string, value interface{}) Error

Any adds some named value into the context

func (Error) As added in v0.2.0

func (e Error) As(target interface{}) bool

As errors.As support method

func (Error) Bool added in v0.2.0

func (e Error) Bool(name string, value bool) Error

Bool adds boolean named value into the context

func (Error) Error added in v0.2.0

func (e Error) Error() string

func (Error) Float32 added in v0.2.0

func (e Error) Float32(name string, value float32) Error

Float32 adds float32 named value into the context

func (Error) Float64 added in v0.2.0

func (e Error) Float64(name string, value float64) Error

Float64 adds float64 named value into the context

func (Error) Int added in v0.2.0

func (e Error) Int(name string, value int) Error

Int adds int named value into the context

func (Error) Int16 added in v0.2.0

func (e Error) Int16(name string, value int16) Error

Int16 adds int16 named value into the context

func (Error) Int32 added in v0.2.0

func (e Error) Int32(name string, value int32) Error

Int32 adds int32 named value into the context

func (Error) Int64 added in v0.2.0

func (e Error) Int64(name string, value int64) Error

Int64 adds int64 named value into the context

func (Error) Int8 added in v0.2.0

func (e Error) Int8(name string, value int8) Error

Int8 adds int8 named value into the context

func (Error) Is added in v0.2.0

func (e Error) Is(err error) bool

Is errors.Is support method

func (Error) Loc added in v0.2.1

func (e Error) Loc(depth int) Error

Loc adds error location into the context

func (Error) Pfx added in v0.3.0

func (e Error) Pfx(prefix string) Error

Pfx adds (replaces) prefix in the rest of the chain.

func (Error) Stg added in v0.2.0

func (e Error) Stg(name string, value fmt.Stringer) Error

Stg adds named value of the given stringer into the context

func (Error) Str added in v0.2.0

func (e Error) Str(name string, value string) Error

Str adds string named value into the context

func (Error) Strs added in v0.2.0

func (e Error) Strs(name string, value []string) Error

Strs adds named slice of strings value into the context

func (Error) Type added in v0.4.0

func (e Error) Type(name string, typ any) Error

Type adds type name into the context

func (Error) Uint added in v0.2.0

func (e Error) Uint(name string, value uint) Error

Uint adds uint named value into the context

func (Error) Uint16 added in v0.2.0

func (e Error) Uint16(name string, value uint16) Error

Uint16 adds uint16 named value into the context

func (Error) Uint32 added in v0.2.0

func (e Error) Uint32(name string, value uint32) Error

Uint32 adds uint32 named value into the context

func (Error) Uint64 added in v0.2.0

func (e Error) Uint64(name string, value uint64) Error

Uint64 adds uint64 named value into the context

func (Error) Uint8 added in v0.2.0

func (e Error) Uint8(name string, value uint8) Error

Uint8 adds uint8 named value into the context

func (Error) Unwrap added in v0.2.0

func (e Error) Unwrap() error

Unwrap returns naked error out of these wraps

type ErrorContextConsumer added in v0.2.0

type ErrorContextConsumer interface {
	Bool(name string, value bool)
	Int(name string, value int)
	Int8(name string, value int8)
	Int16(name string, value int16)
	Int32(name string, value int32)
	Int64(name string, value int64)
	Uint(name string, value uint)
	Uint8(name string, value uint8)
	Uint16(name string, value uint16)
	Uint32(name string, value uint32)
	Uint64(name string, value uint64)
	Float32(name string, value float32)
	Float64(name string, value float64)
	String(name string, value string)
	Any(name string, value interface{})
}

ErrorContextConsumer an abstraction meant to consume structured context of an error.

type ErrorContextDeliverer added in v0.2.0

type ErrorContextDeliverer interface {
	Deliver(cons ErrorContextConsumer)
	Error() string
}

ErrorContextDeliverer an abstraction to work with ErrorContextConsumer, it delivers structured context variables into a consumer.

func GetContextDeliverer added in v0.2.0

func GetContextDeliverer(err error) ErrorContextDeliverer

GetContextDeliverer extracts deliverer from err's chain if it does exist there

Example
package main

import (
	"bytes"
	"fmt"
	"math"

	"github.com/sirkon/errors"
)

func main() {
	err := errors.New("error").
		Bool("b", true).
		Int("i", -1).
		Int8("i8", math.MaxInt8).
		Int16("i16", math.MinInt16).
		Int32("i32", math.MinInt32).
		Int64("i64", math.MinInt64).
		Uint("u", 1).
		Uint8("u8", math.MaxUint8).
		Uint16("u16", math.MaxUint16).
		Uint32("u32", math.MaxUint32).
		Uint64("u64", math.MaxUint64).
		Float32("f32", math.MaxFloat32).
		Float64("f64", math.MaxFloat64).
		Str("string", "str").
		Strs("strings", []string{"1", "2", "3"}).
		Stg("stringer", testStringer{}).
		Any("object", map[string]int{
			"key": 12,
		}).
		Any("bytes", []byte("123")).
		Type("type-name", bytes.NewBuffer(nil))
	err = errors.Wrap(err, "wrapping context").Str("wrapped", "value")
	d := errors.GetContextDeliverer(err)
	var cons testContextConsumer
	d.Deliver(cons)

	if errors.GetContextDeliverer(errors.Const("error")) != nil {
		fmt.Println("must no be here")
	}
}

type testContextConsumer struct{}

func (t testContextConsumer) Bool(name string, value bool) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int(name string, value int) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int8(name string, value int8) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int16(name string, value int16) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int32(name string, value int32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Int64(name string, value int64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint(name string, value uint) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint8(name string, value uint8) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint16(name string, value uint16) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint32(name string, value uint32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Uint64(name string, value uint64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Float32(name string, value float32) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Float64(name string, value float64) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) String(name string, value string) {
	fmt.Printf("%s: %v\n", name, value)
}
func (t testContextConsumer) Any(name string, value interface{}) {
	fmt.Printf("%s: %v\n", name, value)
}

type testStringer struct{}

func (testStringer) String() string {
	return "test stringer"
}
Output:

wrapped: value
b: true
i: -1
i8: 127
i16: -32768
i32: -2147483648
i64: -9223372036854775808
u: 1
u8: 255
u16: 65535
u32: 4294967295
u64: 18446744073709551615
f32: 3.4028235e+38
f64: 1.7976931348623157e+308
string: str
strings: [1 2 3]
stringer: test stringer
object: map[key:12]
bytes: 123
type-name: *bytes.Buffer

Jump to

Keyboard shortcuts

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