-
Notifications
You must be signed in to change notification settings - Fork 568
/
interceptor.go
73 lines (63 loc) · 1.74 KB
/
interceptor.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
66
67
68
69
70
71
72
73
package errors
import (
"context"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// UnaryServerInterceptor translates errors for unary RPCs
func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
res, err := handler(ctx, req)
return res, errorForGRPC(err)
}
// StreamServerInterceptor translates errors for streaming RPCs
func StreamServerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, stream)
return errorForGRPC(err)
}
func errorForGRPC(err error) error {
// strip off the initial stack trace layer, in case this is just a once-wrapped auth error
cursor := err
for cursor != nil {
if _, ok := cursor.(errors.StackTracer); ok {
cursor = errors.Unwrap(cursor)
} else {
break
}
}
if cursor == nil && err != nil {
// this should never happen, we unwrapped an error consisting only of stack traces
// reset to the original error, we have no idea what this is
cursor = err
}
if _, ok := status.FromError(cursor); ok {
// handles nil as well
return cursor
}
code := codes.Unknown
for cursor != nil {
if res, ok := status.FromError(cursor); ok {
code = res.Code()
break
}
cursor = errors.Unwrap(cursor)
}
return &gRPCStatusError{
code: code,
err: err,
}
}
type gRPCStatusError struct {
err error
code codes.Code
}
func (e *gRPCStatusError) GRPCStatus() *status.Status {
return status.New(e.code, e.Error())
}
func (e *gRPCStatusError) Error() string {
return e.err.Error()
}
func (e *gRPCStatusError) Unwrap() error {
return e.err
}