From 70cdb97db4f8b4db80f9d2f071f80d24916de9e7 Mon Sep 17 00:00:00 2001 From: goncalo-rodrigues Date: Thu, 2 Nov 2023 10:46:38 +0100 Subject: [PATCH] [OGE-5650] Return not found status code --- app.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 9ce4b122256..2cea0c9d80a 100644 --- a/app.go +++ b/app.go @@ -610,7 +610,7 @@ func (h *Headscale) Serve() error { } // Start the local gRPC server without TLS and without authentication - grpcSocket := grpc.NewServer(zerolog.UnaryInterceptor()) + grpcSocket := grpc.NewServer(zerolog.UnaryInterceptor(), grpc.UnaryInterceptor(serverErrorInterceptor)) v1.RegisterHeadscaleServiceServer(grpcSocket, newHeadscaleV1APIServer(h)) reflection.Register(grpcSocket) @@ -991,3 +991,13 @@ func readOrCreatePrivateKey(path string) (*key.MachinePrivate, error) { return &machineKey, nil } + +func serverErrorInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { + resp, err := handler(ctx, req) + + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp, status.Error(codes.NotFound, err.Error()) + } + + return resp, err +}