2019-01-14 01:08:38 +00:00
|
|
|
// Package merr extends the errors package with features like key-value
|
|
|
|
// attributes for errors, embedded stacktraces, and multi-errors.
|
2019-01-24 21:45:38 +00:00
|
|
|
//
|
|
|
|
// merr functions takes in generic errors of the built-in type. The returned
|
|
|
|
// errors are wrapped by a type internal to merr, and appear to also be of the
|
2021-04-09 23:30:59 +00:00
|
|
|
// generic error type.
|
|
|
|
//
|
|
|
|
// As is generally recommended for go projects, errors.Is and errors.As should
|
|
|
|
// be used for equality checking.
|
2019-01-14 01:08:38 +00:00
|
|
|
package merr
|
|
|
|
|
|
|
|
import (
|
2019-02-09 19:08:30 +00:00
|
|
|
"context"
|
2019-01-14 01:08:38 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
2019-01-14 01:08:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var strBuilderPool = sync.Pool{
|
|
|
|
New: func() interface{} { return new(strings.Builder) },
|
|
|
|
}
|
|
|
|
|
|
|
|
func putStrBuilder(sb *strings.Builder) {
|
|
|
|
sb.Reset()
|
|
|
|
strBuilderPool.Put(sb)
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
type annotateKey string
|
|
|
|
|
|
|
|
// Error wraps an error such that contextual and stacktrace information is
|
|
|
|
// captured alongside that error.
|
|
|
|
type Error struct {
|
|
|
|
Err error
|
|
|
|
Ctx context.Context
|
|
|
|
Stacktrace Stacktrace
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// Error implements the method for the error interface.
|
|
|
|
func (e Error) Error() string {
|
2021-04-10 00:05:07 +00:00
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullError returns an error string which includes contextual annotations and
|
|
|
|
// stacktrace information.
|
|
|
|
func (e Error) FullError() string {
|
|
|
|
sb := new(strings.Builder)
|
|
|
|
sb.WriteString(strings.TrimSpace(e.Error()))
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
annotations := make(mctx.Annotations)
|
|
|
|
mctx.EvaluateAnnotations(e.Ctx, annotations)
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
annotations[annotateKey("line")] = e.Stacktrace.String()
|
2019-01-24 21:45:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
for _, kve := range annotations.StringSlice(true) {
|
|
|
|
k, v := strings.TrimSpace(kve[0]), strings.TrimSpace(kve[1])
|
|
|
|
sb.WriteString("\n\t* ")
|
|
|
|
sb.WriteString(k)
|
|
|
|
sb.WriteString(": ")
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// if there's no newlines then print v inline with k
|
|
|
|
if strings.Index(v, "\n") < 0 {
|
|
|
|
sb.WriteString(v)
|
|
|
|
continue
|
|
|
|
}
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
for _, vLine := range strings.Split(v, "\n") {
|
|
|
|
sb.WriteString("\n\t\t")
|
|
|
|
sb.WriteString(strings.TrimSpace(vLine))
|
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
return sb.String()
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// Unwrap implements the method for the errors package.
|
|
|
|
func (e Error) Unwrap() error {
|
|
|
|
return e.Err
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WrapSkip is like Wrap but also allows for skipping extra stack frames when
|
|
|
|
// embedding the stack into the error.
|
2021-04-09 23:30:59 +00:00
|
|
|
func WrapSkip(ctx context.Context, err error, skip int) error {
|
|
|
|
if err == nil {
|
2019-02-27 18:05:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
if e := (Error{}); errors.As(err, &e) {
|
|
|
|
e.Err = err
|
|
|
|
e.Ctx = mctx.MergeAnnotations(e.Ctx, ctx)
|
|
|
|
return e
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
return Error{
|
|
|
|
Err: err,
|
|
|
|
Ctx: ctx,
|
|
|
|
Stacktrace: newStacktrace(skip + 1),
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
}
|
2019-01-14 01:08:38 +00:00
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// Wrap returns a copy of the given error wrapped in an Error. If the given
|
|
|
|
// error is already wrapped in an *Error then the given context is merged into
|
|
|
|
// that one with mctx.MergeAnnotations instead.
|
2019-02-27 18:05:51 +00:00
|
|
|
//
|
|
|
|
// Wrapping nil returns nil.
|
2021-04-09 23:30:59 +00:00
|
|
|
func Wrap(ctx context.Context, err error) error {
|
|
|
|
return WrapSkip(ctx, err, 1)
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// New is a shortcut for:
|
2021-04-09 23:30:59 +00:00
|
|
|
// merr.WrapSkip(ctx, errors.New(str), 1)
|
|
|
|
func New(ctx context.Context, str string) error {
|
|
|
|
return WrapSkip(ctx, errors.New(str), 1)
|
2019-01-14 01:08:38 +00:00
|
|
|
}
|