errors

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: MIT Imports: 5 Imported by: 0

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 interface{}) 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")
	}

}

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

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 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 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 with 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

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

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

func (Error) Float64 added in v0.2.0

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

func (Error) Int added in v0.2.0

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

func (Error) Int8 added in v0.2.0

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

func (Error) Int16 added in v0.2.0

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

func (Error) Int32 added in v0.2.0

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

func (Error) Int64 added in v0.2.0

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

func (Error) Is added in v0.2.0

func (e Error) Is(err error) bool

Is errors.Is support method

func (Error) Stg added in v0.2.0

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

func (Error) Str added in v0.2.0

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

func (Error) Strs added in v0.2.0

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

func (Error) Uint added in v0.2.0

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

func (Error) Uint8 added in v0.2.0

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

func (Error) Uint16 added in v0.2.0

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

func (Error) Uint32 added in v0.2.0

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

func (Error) Uint64 added in v0.2.0

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

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 (
	"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"))
	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

Jump to

Keyboard shortcuts

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