From 46a417f3a3acee3cebb3197884d37dacacb2b293 Mon Sep 17 00:00:00 2001 From: Neniel <11655196+Neniel@users.noreply.github.com> Date: Fri, 16 Jun 2023 07:38:33 -0300 Subject: [PATCH] reduce branching when handling grpc errors --- pkg/agent/client/client.go | 55 +++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/pkg/agent/client/client.go b/pkg/agent/client/client.go index 2b888b12aa5..beb3bdb0318 100644 --- a/pkg/agent/client/client.go +++ b/pkg/agent/client/client.go @@ -171,16 +171,15 @@ func (c *client) RenewSVID(ctx context.Context, csr []byte) (*X509SVID, error) { }) if err != nil { c.release(connection) - errToReturn := fmt.Errorf("failed to renew agent: %w", err) + log := c.c.Log if s, ok := status.FromError(err); ok { - c.c.Log.WithFields(logrus.Fields{ + log = log.WithFields(logrus.Fields{ "code": s.Code(), "message": s.Message(), - }).WithError(err).Error("Failed to renew agent") - return nil, errToReturn + }) } - c.c.Log.WithError(err).Error("Failed to renew agent") - return nil, errToReturn + log.WithError(err).Error("Failed to renew agent") + return nil, fmt.Errorf("failed to renew agent: %w", err) } var certChain []byte @@ -253,16 +252,15 @@ func (c *client) NewJWTSVID(ctx context.Context, entryID string, audience []stri }) if err != nil { c.release(connection) - errToReturn := fmt.Errorf("failed to fetch JWT SVID: %w", err) + log := c.c.Log if s, ok := status.FromError(err); ok { - c.c.Log.WithFields(logrus.Fields{ + log = log.WithFields(logrus.Fields{ "code": s.Code(), "message": s.Message(), - }).WithError(err).Error("Failed to fetch JWT SVID") - return nil, errToReturn + }) } - c.c.Log.WithError(err).Error("Failed to fetch JWT SVID") - return nil, errToReturn + log.WithError(err).Error("Failed to fetch JWT SVID") + return nil, fmt.Errorf("failed to fetch JWT SVID: %w", err) } svid := resp.Svid @@ -341,16 +339,15 @@ func (c *client) fetchEntries(ctx context.Context) ([]*types.Entry, error) { }) if err != nil { c.release(connection) - errToReturn := fmt.Errorf("failed to fetch authorized entries: %w", err) + log := c.c.Log if s, ok := status.FromError(err); ok { - c.c.Log.WithFields(logrus.Fields{ + log = log.WithFields(logrus.Fields{ "code": s.Code(), "message": s.Message(), - }).WithError(err).Error("Failed to fetch authorized entries") - return nil, errToReturn + }) } - c.c.Log.WithError(err).Error("Failed to fetch authorized entries") - return nil, errToReturn + log.WithError(err).Error("Failed to fetch authorized entries") + return nil, fmt.Errorf("failed to fetch authorized entries: %w", err) } return resp.Entries, err @@ -369,16 +366,15 @@ func (c *client) fetchBundles(ctx context.Context, federatedBundles []string) ([ bundle, err := bundleClient.GetBundle(ctx, &bundlev1.GetBundleRequest{}) if err != nil { c.release(connection) - errToReturn := fmt.Errorf("failed to fetch bundle: %w", err) + log := c.c.Log if s, ok := status.FromError(err); ok { - c.c.Log.WithFields(logrus.Fields{ + log = log.WithFields(logrus.Fields{ "code": s.Code(), "message": s.Message(), - }).WithError(err).Error("Failed to fetch bundle") - return nil, errToReturn + }) } - c.c.Log.WithError(err).Error("Failed to fetch bundle") - return nil, errToReturn + log.WithError(err).Error("Failed to fetch bundle") + return nil, fmt.Errorf("failed to fetch bundle: %w", err) } bundles = append(bundles, bundle) @@ -416,16 +412,15 @@ func (c *client) fetchSVIDs(ctx context.Context, params []*svidv1.NewX509SVIDPar }) if err != nil { c.release(connection) - errToReturn := fmt.Errorf("failed to batch new X509 SVID(s): %w", err) + log := c.c.Log if s, ok := status.FromError(err); ok { - c.c.Log.WithFields(logrus.Fields{ + log = log.WithFields(logrus.Fields{ "code": s.Code(), "message": s.Message(), - }).WithError(err).Error("Failed to batch new X509 SVID(s)") - return nil, errToReturn + }) } - c.c.Log.WithError(err).Error("Failed to batch new X509 SVID(s)") - return nil, errToReturn + log.WithError(err).Error("Failed to batch new X509 SVID(s)") + return nil, fmt.Errorf("failed to batch new X509 SVID(s): %w", err) } okStatus := int32(codes.OK)