-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgrpc_unwrap_interceptors.go
43 lines (35 loc) · 1.21 KB
/
grpc_unwrap_interceptors.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package herodot
import (
"context"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
type grpcStatusError interface {
error
GRPCStatus() *status.Status
}
func unwrapGRPCStatusError(err error) error {
if err == nil {
return nil
}
var innerErr grpcStatusError
if errors.As(err, &innerErr) {
return innerErr
}
return err
}
// UnaryErrorUnwrapInterceptor is a gRPC server-side interceptor that unwraps herodot errors for Unary RPCs.
// See https://github.com/grpc/grpc-go/issues/2934 for why this is necessary.
func UnaryErrorUnwrapInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
return resp, unwrapGRPCStatusError(err)
}
// StreamErrorUnwrapInterceptor is a gRPC server-side interceptor that unwraps herodot errors for Streaming RPCs.
// See https://github.com/grpc/grpc-go/issues/2934 for why this is necessary.
func StreamErrorUnwrapInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, ss)
return unwrapGRPCStatusError(err)
}