This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
66 lines (60 loc) · 2.24 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
package grpc
import (
"runtime/debug"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/api/errors"
)
// PanicLoggerUnaryServerInterceptor returns a new unary server interceptor for recovering from panics and returning error
func PanicLoggerUnaryServerInterceptor(log *log.Entry) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ interface{}, err error) {
defer func() {
if r := recover(); r != nil {
log.Errorf("Recovered from panic: %+v\n%s", r, debug.Stack())
err = status.Errorf(codes.Internal, "%s", r)
}
}()
return handler(ctx, req)
}
}
// PanicLoggerStreamServerInterceptor returns a new streaming server interceptor for recovering from panics and returning error
func PanicLoggerStreamServerInterceptor(log *log.Entry) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
defer func() {
if r := recover(); r != nil {
log.Errorf("Recovered from panic: %+v\n%s", r, debug.Stack())
err = status.Errorf(codes.Internal, "%s", r)
}
}()
return handler(srv, stream)
}
}
// translate a K8S errors into gRPC error - assume that we want to surface this - which we may not
func translateError(err error) error {
switch e := err.(type) {
case *errors.StatusError:
switch e.Status().Code {
case 400:
return status.Error(codes.InvalidArgument, e.Error())
case 401:
return status.Error(codes.Unauthenticated, e.Error())
case 403:
return status.Error(codes.PermissionDenied, e.Error())
case 404:
return status.Error(codes.NotFound, e.Error())
}
}
return err
}
var (
ErrorTranslationUnaryServerInterceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
return resp, translateError(err)
}
ErrorTranslationStreamServerInterceptor = func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return translateError(handler(srv, ss))
}
)