-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
65 lines (54 loc) · 1.61 KB
/
error.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package kafka
import (
"context"
)
// Error is an error with an associated [Message] providing additional context.
type Error struct {
Message Message
Err error
}
// Error returns the original error message without modifications.
func (err Error) Error() string {
return err.Err.Error()
}
// Unwrap returns the original error.
func (err Error) Unwrap() error {
return err.Err
}
// Handler is an interface for processing errors.
type ErrorHandler interface {
Handle(ctx context.Context, err error) error
}
// ErrorHandlerFunc is an adapter type that allows the use of ordinary functions as an [ErrorHandler].
type ErrorHandlerFunc func(ctx context.Context, err error) error
// Handle calls itself passing all arguments through.
func (fn ErrorHandlerFunc) Handle(ctx context.Context, err error) error {
return fn(ctx, err)
}
// WrapErrorMiddleware returns a middleware that wraps errors with additional context using the Error type.
func WrapErrorMiddleware() Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, msg Message) error {
if err := next.Handle(ctx, msg); err != nil {
return Error{
Message: msg,
Err: err,
}
}
return nil
})
}
}
// CatchErrorMiddleware returns a middleware that catches and handles errors with an [ErrorHandler].
func CatchErrorMiddleware(eh ErrorHandler) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, msg Message) error {
if err := next.Handle(ctx, msg); err != nil {
if err := eh.Handle(ctx, err); err != nil {
return err
}
}
return nil
})
}
}