-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
41 lines (32 loc) · 847 Bytes
/
context.go
File metadata and controls
41 lines (32 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package errors
import (
"context"
"github.com/pkg/errors"
)
// ContextError represents a standard error
// that can also encapsulate a context.
type ContextError struct {
Err error
Ctx context.Context //nolint:containedctx
}
func WithContext(err error, ctx context.Context) *ContextError {
return &ContextError{
Err: err,
Ctx: ctx,
}
}
func WrapContext(err error, ctx context.Context, message string) *ContextError {
return WithContext(errors.Wrap(err, message), ctx)
}
func WrapfContext(err error, ctx context.Context, format string, args ...any) *ContextError {
return WithContext(errors.Wrapf(err, format, args...), ctx)
}
func (ce *ContextError) Error() string {
return ce.Err.Error()
}
func (ce *ContextError) Cause() error {
return errors.Cause(ce.Unwrap())
}
func (ce *ContextError) Unwrap() error {
return ce.Err
}