Documentation
¶
Index ¶
- func As(err error, target any) bool
- func Dig[T error](err error) *T
- func Is(err, target error) bool
- func Join(errs ...error) error
- type Const
- type Error
- func (e Error) Any(name string, value interface{}) Error
- func (e Error) As(target interface{}) bool
- func (e Error) Bool(name string, value bool) Error
- func (e Error) Error() string
- func (e Error) Float32(name string, value float32) Error
- func (e Error) Float64(name string, value float64) Error
- func (e Error) Int(name string, value int) Error
- func (e Error) Int16(name string, value int16) Error
- func (e Error) Int32(name string, value int32) Error
- func (e Error) Int64(name string, value int64) Error
- func (e Error) Int8(name string, value int8) Error
- func (e Error) Is(err error) bool
- func (e Error) Loc(depth int) Error
- func (e Error) Pfx(prefix string) Error
- func (e Error) Stg(name string, value fmt.Stringer) Error
- func (e Error) Str(name string, value string) Error
- func (e Error) Strs(name string, value []string) Error
- func (e Error) Type(name string, typ any) Error
- func (e Error) Uint(name string, value uint) Error
- func (e Error) Uint16(name string, value uint16) Error
- func (e Error) Uint32(name string, value uint32) Error
- func (e Error) Uint64(name string, value uint64) Error
- func (e Error) Uint8(name string, value uint8) Error
- func (e Error) Unwrap() error
- type ErrorContextConsumer
- type ErrorContextDeliverer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dig ¶ added in v0.2.0
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 ¶
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:
Types ¶
type Const ¶ added in v0.2.0
type Const string
Const an implementation of error that can be a constant
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
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 ¶
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 ¶
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 ¶
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 ¶
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
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