Documentation
¶
Index ¶
- func And(err1 error, err2 error) error
- func As(err error, target interface{}) bool
- func CtxLogger(ctx context.Context, err error) *zerolog.Logger
- func Is(err, target error) bool
- func Logger(logger zerolog.Logger, err error) *zerolog.Logger
- func New(msg string) error
- func Newf(format string, a ...interface{}) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, a ...interface{}) error
- type ContextBuilder
- type ContextReportBuilder
- type List
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶
And creates a new combined error. Avoid `err = errors.And(err1, err)` and use `err = errors.And(err, err1)` instead when collecting numerous errors. This will save some memory allocations.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.And(io.EOF, io.EOF))
fmt.Println(errors.And(nil, io.EOF))
fmt.Println(errors.And(io.EOF, nil))
fmt.Println(errors.List{io.EOF})
}
Output: EOF; EOF EOF EOF EOF
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.
As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
var ce customError
err := errors.Wrap(customError("message"), "message")
err = customWrapper{err: err}
if errors.As(err, &ce) {
fmt.Println(ce)
}
if !errors.As(err, &fakeError{}) {
fmt.Println(err)
}
errs := errors.And(io.EOF, err)
errs = errors.And(errs, io.EOF)
if errors.As(errs, &ce) {
fmt.Println(ce)
}
fmt.Println(errors.As(errs, &fakeError{}))
}
type customWrapper struct {
err error
}
func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }
type customError string
func (ce customError) Error() string { return "error " + string(ce) }
type fakeError struct{}
func (ce fakeError) Error() string { return "fake error" }
Output: error message custom wrap: message: error message error message false
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
ce := customError("message")
err := errors.Wrap(customError("message"), "message")
err = customWrapper{err: err}
fmt.Println(errors.Is(err, ce))
fmt.Println(errors.Is(err, fakeError{}))
errs := errors.And(io.EOF, err)
errs = errors.And(errs, io.EOF)
fmt.Println(errors.Is(errs, ce))
fmt.Println(errors.Is(errs, fakeError{}))
fmt.Println("checking nil cases")
fmt.Println(errors.Is(nil, nil))
fmt.Println(errors.Is(io.EOF, nil))
fmt.Println(errors.Is(nil, io.EOF))
}
type customWrapper struct {
err error
}
func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }
type customError string
func (ce customError) Error() string { return "error " + string(ce) }
type fakeError struct{}
func (ce fakeError) Error() string { return "fake error" }
Output: true false true false checking nil cases true false false
func Logger ¶ added in v0.1.0
Logger возвращаем логгер аннотированный, возможно, значениями из контекста ошибки (если он заполнен)
Example ¶
package main
import (
"os"
"github.com/rs/zerolog"
"github.com/sirkon/errors"
)
func main() {
logger := zerolog.New(os.Stdout).Level(zerolog.DebugLevel)
err := errors.Context().Int64("epoch", 1234).String("user-id", "1").New("no such user this time")
err = errors.Wrap(err, "check if user can be synced")
errors.Logger(logger, err).Error().Msg("syncing user data failed")
err = errors.Context().
Int("i", 1).
Int8("i8", 1).
Int16("i16", 1).
Int32("i32", 1).
Int64("i64", 1).
Uint("u", 1).
Uint8("u8", 1).
Uint16("u16", 1).
Uint32("u32", 1).
Uint64("u64", 1).
String("s", "string").
Strings("ss", []string{"1"}).
Any("a", map[string]int{}).
Line().
New("message")
errors.Logger(logger, err).Error().Msg("all types")
}
Output: {"level":"error","error":"check if user can be synced: no such user this time","epoch":1234,"user-id":"1","message":"syncing user data failed"} {"level":"error","error":"message","err-pos":"/home/emacs/go/src/github.com/sirkon/errors/logger_test.go:30","i":1,"i8":1,"i16":1,"i32":1,"i64":1,"u":1,"u8":1,"u16":1,"u32":1,"u64":1,"s":"string","ss":["1"],"a":{},"message":"all types"}
func New ¶
New just an alias for stdlib's New
Example ¶
package main
import (
"fmt"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.New("error"))
}
Output: error
func Newf ¶
Newf an alias for fmt.Errorf
Example ¶
package main
import (
"fmt"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.Newf("error %s", "message"))
}
Output: error message
func Wrap ¶
Wrap consturcts a new error by wrapping given message into an error
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.Wrap(io.EOF, "error"))
fmt.Println(errors.Wrap(errors.Wrapf(io.EOF, "msg %d", 1), "error"))
}
Output: error: EOF error: msg 1: EOF
Types ¶
type ContextBuilder ¶ added in v0.1.0
type ContextBuilder interface {
New(msg string) error
Newf(format string, a ...interface{}) error
Wrap(err error, msg string) error
Wrapf(err error, format string, a ...interface{}) error
Int(name string, value int) ContextBuilder
Int8(name string, value int8) ContextBuilder
Int16(name string, value int16) ContextBuilder
Int32(name string, value int32) ContextBuilder
Int64(name string, value int64) ContextBuilder
Uint(name string, value uint) ContextBuilder
Uint8(name string, value uint8) ContextBuilder
Uint16(name string, value uint16) ContextBuilder
Uint32(name string, value uint32) ContextBuilder
Uint64(name string, value uint64) ContextBuilder
Float32(name string, value float32) ContextBuilder
Float64(name string, value float64) ContextBuilder
String(name string, value string) ContextBuilder
Strings(name string, values []string) ContextBuilder
Any(name string, value interface{}) ContextBuilder
Line() ContextBuilder
Report(dest ContextReportBuilder)
}
ContextBuilder заполнение контекста информацией и функции завершения построения
type ContextReportBuilder ¶ added in v0.1.0
type ContextReportBuilder interface {
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)
Strings(name string, values []string)
Any(name string, value interface{})
Err(err error)
}
ContextReportBuilder абстракция занесения сущностей сохранённых ContextBuilder в контекст, например, логгера