From 1d02d10f32e477f4bef54bcc55f373b230f7322f Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 15 Aug 2024 10:04:27 +0200 Subject: [PATCH 1/2] fix: show only grpc error Signed-off-by: Miguel --- app/cli/main.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/cli/main.go b/app/cli/main.go index e08d9fc6c..d2388e19d 100644 --- a/app/cli/main.go +++ b/app/cli/main.go @@ -50,11 +50,24 @@ func errorInfo(err error, logger zerolog.Logger) (string, int) { // Extract message from grpc message if applicable st, ok := status.FromError(err) + // It's a regular error if !ok { - // Regular error msg = err.Error() } else { msg = st.Message() + // Extract message from grpc status if possible + // by default status.fromError(err).Message() returns the whole error chain + // i.e "creating API token: creating API token: rpc error: code = AlreadyExists desc = duplicated: name already taken" + // We do not want in this case the whole error chain but just the one part of the gRPC response + // i.e "duplicated: name already taken" + // To do what we perform an additional parsing of the error similar to + // https://github.com/grpc/grpc-go/blob/ced812e3287e15a009eab5b271c25750050a2f82/status/status.go#L123 + type grpcstatus interface{ GRPCStatus() *status.Status } + var gs grpcstatus + if errors.As(err, &gs) { + grpcStatus := gs.GRPCStatus() + msg = grpcStatus.Message() + } } // Make overrides From b65880b91c3b8fe830aec733f37bcd5b92740dc7 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 15 Aug 2024 10:22:25 +0200 Subject: [PATCH 2/2] fix: show only grpc error Signed-off-by: Miguel --- app/cli/main.go | 16 +++++++++++++--- app/controlplane/internal/service/service.go | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/cli/main.go b/app/cli/main.go index d2388e19d..537b636f9 100644 --- a/app/cli/main.go +++ b/app/cli/main.go @@ -27,6 +27,7 @@ import ( _ "github.com/sigstore/sigstore/pkg/signature/kms/azure" _ "github.com/sigstore/sigstore/pkg/signature/kms/gcp" _ "github.com/sigstore/sigstore/pkg/signature/kms/hashivault" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -55,18 +56,27 @@ func errorInfo(err error, logger zerolog.Logger) (string, int) { msg = err.Error() } else { msg = st.Message() - // Extract message from grpc status if possible + // Sanitize error message for validation and other known errors // by default status.fromError(err).Message() returns the whole error chain // i.e "creating API token: creating API token: rpc error: code = AlreadyExists desc = duplicated: name already taken" - // We do not want in this case the whole error chain but just the one part of the gRPC response + // We do not want to show that in some specific error codes, we just want to show the part of the gRPC response // i.e "duplicated: name already taken" // To do what we perform an additional parsing of the error similar to // https://github.com/grpc/grpc-go/blob/ced812e3287e15a009eab5b271c25750050a2f82/status/status.go#L123 type grpcstatus interface{ GRPCStatus() *status.Status } var gs grpcstatus if errors.As(err, &gs) { + knownCodes := []codes.Code{ + codes.AlreadyExists, codes.InvalidArgument, codes.NotFound, codes.PermissionDenied, + } + grpcStatus := gs.GRPCStatus() - msg = grpcStatus.Message() + for _, code := range knownCodes { + if st.Code() == code { + msg = grpcStatus.Message() + break + } + } } } diff --git a/app/controlplane/internal/service/service.go b/app/controlplane/internal/service/service.go index c257ba439..124dc0d92 100644 --- a/app/controlplane/internal/service/service.go +++ b/app/controlplane/internal/service/service.go @@ -134,6 +134,9 @@ func WithLogger(logger log.Logger) NewOpt { } } +// NOTE: some of these http errors get automatically translated to gRPC status codes +// because they implement the gRPC status error interface +// so it is safe to return either a gRPC status error or a kratos error func handleUseCaseErr(err error, l *log.Helper) error { switch { case errors.Is(err, context.Canceled):