Skip to content

Commit 66e8901

Browse files
committed
Fix error code forwarding
1 parent 2ad43f9 commit 66e8901

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cmd/module/module.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"google.golang.org/grpc"
1717
"google.golang.org/grpc/credentials"
1818
"google.golang.org/grpc/credentials/insecure"
19+
"google.golang.org/grpc/status"
1920
)
2021

2122
type backend struct {
@@ -651,7 +652,17 @@ func init() {
651652
// Create a new TLS credentials based on the TLS configuration
652653
c = credentials.NewTLS(tlsConfig)
653654
}
654-
conn, err := grpc.Dial(os.Getenv("PKCS11_PROXY_URI"), grpc.WithTransportCredentials(c))
655+
errHandler := func(ctx context.Context, method string, req, resp interface{}, info *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
656+
err := invoker(ctx, method, req, resp, info, opts...)
657+
if err != nil {
658+
s := status.Convert(err)
659+
if s != nil {
660+
err = pkcs11.Error(s.Code())
661+
}
662+
}
663+
return err
664+
}
665+
conn, err := grpc.Dial(os.Getenv("PKCS11_PROXY_URI"), grpc.WithTransportCredentials(c), grpc.WithUnaryInterceptor(errHandler))
655666
if err != nil {
656667
log.Fatalf("unable to connect to pkcs11 proxy using env PKCS11_PROXY_URI %s: %s", os.Getenv("PKCS11_PROXY_URI"), err)
657668
}

cmd/server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
p11 "github.com/ryarnyah/pkcs11-go-proxy/pkcs11"
1717
"github.com/ryarnyah/pkcs11-go-proxy/pkg"
1818
grpc "google.golang.org/grpc"
19+
"google.golang.org/grpc/codes"
1920
"google.golang.org/grpc/credentials"
2021
"google.golang.org/grpc/credentials/insecure"
22+
"google.golang.org/grpc/status"
2123
)
2224

2325
// ErrCtxNotFound raised when context can't be found.
@@ -1038,6 +1040,10 @@ func main() {
10381040
errHandler := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
10391041
resp, err := handler(ctx, req)
10401042
if err != nil {
1043+
var pe pkcs11.Error
1044+
if errors.As(err, &pe) {
1045+
err = status.Error(codes.Code(pe), err.Error())
1046+
}
10411047
log.Printf("method %q failed: %s", info.FullMethod, err)
10421048
}
10431049
return resp, err

0 commit comments

Comments
 (0)