Skip to content

Commit

Permalink
add error logging for failed rollbacks and improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanulit committed Nov 22, 2024
1 parent 032cb12 commit b734f49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions service/policy/attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions service/policy/db/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit b734f49

Please sign in to comment.