Skip to content

Add rule to prevent bad rescue usage inside a transaction #1386

Open
@deanylev

Description

@deanylev

Is your feature request related to a problem? Please describe.

A common mistake made in our Rails codebase is:

    ActiveRecord::Base.transaction do
      # create a few records
    rescue StandardError => e
      # we've rescued too early, meaning no rollback will occur!
    end

Instead of

    begin
      ActiveRecord::Base.transaction do
        # create a few records
      end
    rescue StandardError => e
      # the rollback has already occurred by this point, so we're good to go
    end

Describe the solution you'd like

A RuboCop rule to prevent this would be brilliant, as the effects to data integrity in production can be devastating

Describe alternatives you've considered

I have tried writing a rule myself, to no avail

Additional context

Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions