-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_server_error_handlers.go
More file actions
37 lines (31 loc) · 1.4 KB
/
stream_server_error_handlers.go
File metadata and controls
37 lines (31 loc) · 1.4 KB
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"
)
// StreamServerFailHandlerFunc is a function that called by stream server interceptors when specified application erorrs are detected.
type StreamServerFailHandlerFunc func(context.Context, interface{}, interface{}, *grpc.StreamServerInfo, *fail.Error) error
type streamServerFailHandler struct {
f StreamServerFailHandlerFunc
}
func (h *streamServerFailHandler) HandleStreamServerError(c context.Context, req interface{}, resp interface{}, info *grpc.StreamServerInfo, err error) error {
fErr := fail.Unwrap(err)
if fErr != nil {
return h.f(c, req, resp, info, fErr)
}
return err
}
// WithStreamServerFailHandler returns a new error handler for stream servers for handling errors wrapped with fail.Error.
func WithStreamServerFailHandler(f StreamServerFailHandlerFunc) StreamServerErrorHandler {
return &streamServerFailHandler{f: f}
}
// WithStreamServerReportableErrorHandler returns a new error handler for stream servers for handling errors annotated with the reportability.
func WithStreamServerReportableErrorHandler(f StreamServerFailHandlerFunc) StreamServerErrorHandler {
return WithStreamServerFailHandler(func(c context.Context, req interface{}, resp interface{}, info *grpc.StreamServerInfo, err *fail.Error) error {
if err.Ignorable {
return err
}
return f(c, req, resp, info, err)
})
}