-
Notifications
You must be signed in to change notification settings - Fork 0
/
unary_server_error_handlers.go
37 lines (31 loc) · 1.32 KB
/
unary_server_error_handlers.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
package grpcerrors
import (
"github.com/srvc/fail/v4"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// UnaryServerFailHandlerFunc is a function that called by unary server interceptors when specified application erorrs are detected.
type UnaryServerFailHandlerFunc func(context.Context, interface{}, *grpc.UnaryServerInfo, *fail.Error) error
type unaryServerFailHandler struct {
f UnaryServerFailHandlerFunc
}
func (h *unaryServerFailHandler) HandleUnaryServerError(c context.Context, req interface{}, info *grpc.UnaryServerInfo, err error) error {
fErr := fail.Unwrap(err)
if fErr != nil {
return h.f(c, req, info, fErr)
}
return err
}
// WithUnaryServerFailHandler returns a new error handler for unary servers for handling errors wrapped with fail.Error.
func WithUnaryServerFailHandler(f UnaryServerFailHandlerFunc) UnaryServerErrorHandler {
return &unaryServerFailHandler{f: f}
}
// WithUnaryServerReportableErrorHandler returns a new error handler for unary servers for handling errors annotated with the reportability.
func WithUnaryServerReportableErrorHandler(f UnaryServerFailHandlerFunc) UnaryServerErrorHandler {
return WithUnaryServerFailHandler(func(c context.Context, req interface{}, info *grpc.UnaryServerInfo, err *fail.Error) error {
if err.Ignorable {
return err
}
return f(c, req, info, err)
})
}