Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show only grpc error #1236

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -50,11 +51,33 @@ 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()
// 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 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()
for _, code := range knownCodes {
if st.Code() == code {
msg = grpcStatus.Message()
break
}
}
}
}

// Make overrides
Expand Down
3 changes: 3 additions & 0 deletions app/controlplane/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading