From b734f496aaebb67aa3777b687989b5fc99504e2e Mon Sep 17 00:00:00 2001 From: Ryan Yanulites Date: Fri, 22 Nov 2024 12:26:09 -0700 Subject: [PATCH] add error logging for failed rollbacks and improve errors --- service/policy/attributes/attributes.go | 4 ++-- service/policy/db/policy.go | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/service/policy/attributes/attributes.go b/service/policy/attributes/attributes.go index c568321d8..dc96c5eb5 100644 --- a/service/policy/attributes/attributes.go +++ b/service/policy/attributes/attributes.go @@ -58,7 +58,7 @@ func (s AttributesService) CreateAttribute(ctx context.Context, item, err := txClient.CreateAttribute(ctx, req.Msg) if err != nil { s.logger.Audit.PolicyCRUDFailure(ctx, auditParams) - return db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("attribute", req.Msg.String())) + return err } s.logger.Debug("created new attribute definition", slog.String("name", req.Msg.GetName())) @@ -71,7 +71,7 @@ func (s AttributesService) CreateAttribute(ctx context.Context, return nil }) if err != nil { - return nil, err + return nil, db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("attribute", req.Msg.String())) } return connect.NewResponse(rsp), nil diff --git a/service/policy/db/policy.go b/service/policy/db/policy.go index 4f4a2e614..2c1cc5338 100644 --- a/service/policy/db/policy.go +++ b/service/policy/db/policy.go @@ -39,13 +39,18 @@ func (c *PolicyDBClient) RunInTx(ctx context.Context, query func(txClient *Polic if err != nil { return fmt.Errorf("failed to begin DB transaction: %w", err) } - //nolint:errcheck // ignore error per sqlc docs https://docs.sqlc.dev/en/stable/howto/transactions.html - defer tx.Rollback(ctx) txClient := &PolicyDBClient{c.Client, c.logger, c.Queries.WithTx(tx), c.listCfg} err = query(txClient) if err != nil { + c.logger.WarnContext(ctx, "error during DB transaction, rolling back") + + if rollbackErr := tx.Rollback(ctx); rollbackErr != nil { + // this should never happen, but if it does, we want to know about it + return fmt.Errorf("failed to rollback DB transaction for [%w]: %w", err, rollbackErr) + } + return err }