fix: grpc status in sentry and validation interceptors (#2012)

This commit is contained in:
Livio Amstutz 2021-07-13 15:42:41 +02:00 committed by GitHub
parent da181b9e8c
commit 87ef8f454a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View File

@ -6,8 +6,7 @@ import (
"github.com/getsentry/sentry-go"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/caos/zitadel/internal/api/grpc/errors"
"google.golang.org/grpc/status"
)
func SentryHandler() grpc.UnaryServerInterceptor {
@ -18,8 +17,17 @@ func SentryHandler() grpc.UnaryServerInterceptor {
func sendErrToSentry(ctx context.Context, req interface{}, handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
code, _, _, _ := errors.ExtractCaosError(err)
if code == codes.Unknown || code == codes.Internal {
code := status.Code(err)
switch code {
case codes.Canceled,
codes.Unknown,
codes.DeadlineExceeded,
codes.ResourceExhausted,
codes.Aborted,
codes.Unimplemented,
codes.Internal,
codes.Unavailable,
codes.DataLoss:
sentry.CaptureException(err)
}
return resp, err

View File

@ -4,6 +4,8 @@ import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
_ "github.com/caos/zitadel/internal/statik"
)
@ -25,7 +27,7 @@ func validate(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
}
err := validate.Validate()
if err != nil {
return nil, err
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return handler(ctx, req)
}

View File

@ -35,9 +35,9 @@ func CreateServer(verifier *authz.TokenVerifier, authConfig authz.Config, lang l
grpc_middleware.ChainUnaryServer(
middleware.DefaultTracingServer(),
middleware.MetricsHandler(metricTypes, grpc_api.Probes...),
middleware.SentryHandler(),
middleware.ErrorHandler(),
middleware.AuthorizationInterceptor(verifier, authConfig),
middleware.SentryHandler(),
middleware.TranslationHandler(lang),
middleware.ValidationHandler(),
middleware.ServiceHandler(),