Skip to content

Commit

Permalink
reduce branching when handling grpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Neniel committed Jun 16, 2023
1 parent 4faa1fb commit 46a417f
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions pkg/agent/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46a417f

Please sign in to comment.