Documentation
¶
Overview ¶
Package awesome_errors provides contextful errors and slog automation to consume and show collected contexts.
Index ¶
- func As(err error, target any) bool
- func AsType[E error](err error) (E, bool)
- func DoNotInsertLocations()
- func InsertLocations()
- func Is(err error, target error) bool
- func Join(errs ...error) error
- func NewSentinel(msg string) error
- func NewSentinelf(format string, a ...any) error
- func SLogFlatContext(err *Error) []slog.Attr
- func SLogTreeContext(err *Error) []slog.Attr
- type Error
- func (e *Error) Any(key string, value any) *Error
- func (e *Error) As(target any) bool
- func (e *Error) Bool(key string, value bool) *Error
- func (e *Error) Bytes(key string, value []byte) *Error
- func (e *Error) Error() string
- func (e *Error) F32(key string, value float32) *Error
- func (e *Error) F64(key string, value float64) *Error
- func (e *Error) I8(key string, value int8) *Error
- func (e *Error) I16(key string, value int16) *Error
- func (e *Error) I32(key string, value int32) *Error
- func (e *Error) I64(key string, value int64) *Error
- func (e *Error) Int(key string, value int) *Error
- func (e *Error) Is(target error) bool
- func (e *Error) Stg(key string, value fmt.Stringer) *Error
- func (e *Error) Str(key, value string) *Error
- func (e *Error) Strs(key string, value []string) *Error
- func (e *Error) U8(key string, value uint8) *Error
- func (e *Error) U16(key string, value uint16) *Error
- func (e *Error) U32(key string, value uint32) *Error
- func (e *Error) U64(key string, value uint64) *Error
- func (e *Error) Uint(key string, value uint) *Error
- type ErrorContextBuilder
- type ErrorContextConsumer
- type ErrorContextDeliverer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsType ¶ added in v1.0.0
AsType mirrors errors.AsType.
func DoNotInsertLocations ¶ added in v1.0.0
func DoNotInsertLocations()
DoNotInsertLocations disables the insertion of error handling positions. This is the default mode.
func InsertLocations ¶ added in v1.0.0
func InsertLocations()
InsertLocations enables the insertion of error handling positions.
WARNING!
Calculating positions is a very expensive operation, so it is highly discouraged to enable position capturing in production. Enable only during local debugging.
The recommended usage pattern is enabling when using "development" logging mode, with colored output and formatting intended for reading.
func NewSentinel ¶ added in v1.2.2
NewSentinel creates static error that can be a sentinel.
func NewSentinelf ¶ added in v1.2.2
NewSentinelf same as NewSentinel, just with a format instead of a static message.
func SLogFlatContext ¶ added in v1.2.2
func SLogTreeContext ¶ added in v1.2.2
SLogTreeContext builds slog tree for logging on the place without ErrorContextConsumer indirect calls overhead.
Types ¶
type Error ¶ added in v0.2.0
type Error struct {
// contains filtered or unexported fields
}
func (*Error) As ¶ added in v0.2.0
As implements support for Is. Remember, *Error instances are ephemeral and cannot be targeted.
func (*Error) Error ¶ added in v0.2.0
Example ¶
errs := []error{
func() error {
err := errors.New("new error")
err = errors.Wrap(err, "wrap")
err = errors.Wrapf(err, "wrap no %d", 2)
return err
}(),
func() error {
return errors.Wrap(io.EOF, "wrap")
}(),
}
for _, err := range errs {
fmt.Println(err)
}
Output: wrap no 2: wrap: new error wrap: EOF
type ErrorContextBuilder ¶ added in v1.2.2
type ErrorContextBuilder interface {
Bool(name string, value bool)
Int64(name string, value int64)
Uint64(name string, value uint64)
Flt64(name string, value float64)
Str(name string, value string)
Any(name string, value any)
Loc(position string)
Finalize()
}
ErrorContextBuilder is the contract for dispatching context values per layer of processing.
type ErrorContextConsumer ¶ added in v0.2.0
type ErrorContextConsumer interface {
New(msg string) ErrorContextBuilder
Wrap(msg string) ErrorContextBuilder
Just() ErrorContextBuilder
}
ErrorContextConsumer is the contract that must be implemented by the logging side to receive the structured values stored in the error.
type ErrorContextDeliverer ¶ added in v0.2.0
type ErrorContextDeliverer interface {
Deliver(cons ErrorContextConsumer)
Error() string
}
ErrorContextDeliverer is what is returned by the GetContextDeliverer function. Example of how to work with slog.
type slogConsumer struct{
fields []any
}
func (c *slogConsumer) Int(name, value int) {
c.fields = append(c.fields, slog.Int(name, value))
}
// And so on for the rest of the methods to implement errors.ErrorContextConsumer.
// Log logs at the Info level.
func Log(msg string, fields []slog.Attr) {
var attrs []any
for _, field := range fields {
// Save all original fields.
attrs = append(attrs, field)
// Need to unwrap errors. For this, we look for fields that contain them.
fv := field.Value.Any()
e, ok := fv.(error)
if !ok {
continue
}
// v contains an error. Try to get context from it.
dlr := errors.GetContextDeliverer(e)
if dlr == nil {
// This is not our error.
continue
}
// Get the context and add the extracted fields.
var errCtx slogConsumer{}
dlr.Deliver(&errCtx)
attrs = append(attrs, errCtx.fields...)
}
slog.Info(msg, attrs...)
}
A working example can be found in ./internal/example/main.go.
func GetContextDeliverer ¶ added in v0.2.0
func GetContextDeliverer(err error) ErrorContextDeliverer
GetContextDeliverer returns the structured-context deliverer for the given error.
Example ¶
package main
import (
"fmt"
errors "awesome-errors"
"awesome-errors/errorsctx"
)
func main() {
err := errors.New("test error").Int("count", 12).Str("text", "Hello")
err = errors.Wrap(err, "check").Uint("unsign", 333).Bytes("actually-text", []byte("World!"))
err = errors.Just(err).Bool("this-is-the-last", true).Bytes("real-raw", []byte{1, 2, 3})
dlvr := errors.GetContextDeliverer(err)
var c errorsctx.Consumer
dlvr.Deliver(&c)
for _, layer := range c.Layers {
fmt.Println(layer)
for _, pair := range layer.Pairs {
fmt.Printf(" %s: %v\n", pair.Key, pair.Value.Any())
}
}
}
Output: NEW: test error count: 12 text: Hello WRAP: check unsign: 333 actually-text: World! CTX this-is-the-last: true real-raw: [1 2 3]
func MustGetContextDeliverer ¶ added in v1.2.2
func MustGetContextDeliverer(err *Error) ErrorContextDeliverer
MustGetContextDeliverer guaranteed delivery on the instance of Error istelf.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package errorsctx provides ready to use generic consumer and slog handlers.
|
Package errorsctx provides ready to use generic consumer and slog handlers. |
|
internal
|
|
|
example
command
Package main provides an example of configuration, usage, and results of working with the library.
|
Package main provides an example of configuration, usage, and results of working with the library. |