Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tracking manual exceptions within transactions #119

Merged
merged 3 commits into from
Mar 11, 2021

Conversation

LALOPSB
Copy link
Contributor

@LALOPSB LALOPSB commented Mar 10, 2021

Summary

Manual errors tracked on transactions (i.e custom validations) were not tracked.
This happened because whenever the transaction failed, it was rollbacked, and as the error tracking depends on the persisting of a record on the db, that record was also rollbacked along the transaction that contained it.

Now we've included a way of allowing the ExceptionHunter::Error to be persisted as a part of a new thread not implicated on the transaction itself, so it (alone, the rest of the transaction will) won't be rollbacked.

This new thread is only created if the tracking happens within a transaction.

Examples:

class Payment < ActiveRecord::Base
  validate :is_friend

  belongs_to :friend, class_name: 'User', foreign_key: :friend_id
  belongs_to :sender, class_name: 'User', foreign_key: :sender_id

  private

  def is_friend
    unless (sender.friends.include?(friend))
      errors.add(sender.username, 'you can make payments only to friends.')
      ExceptionHunter.track(ArgumentError.new('Error tracked within transaction'))
    end
  end
end 

Before:

The whole transaction is rollbacked and no records have been persisted

image

Now:

Only the outer transaction is rollbacked and the ExceptionHunter::Error remains persisted.

image

Copy link
Contributor

@brunvez brunvez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an example in the description? It would help people looking at this in the future

ActiveRecord::Base.connection_pool.with_connection do
create_error(exception, custom_data, user)
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we join it immediately?

@LALOPSB LALOPSB requested a review from brunvez March 10, 2021 19:48
@LALOPSB LALOPSB force-pushed the add_support_for_manual_errors_on_transactions branch from 5666bd3 to 2f3c89f Compare March 10, 2021 19:50
@LALOPSB LALOPSB force-pushed the add_support_for_manual_errors_on_transactions branch from 2f3c89f to c626476 Compare March 10, 2021 20:03
@brunvez brunvez merged commit 6a6b9eb into master Mar 11, 2021
@brunvez brunvez deleted the add_support_for_manual_errors_on_transactions branch March 11, 2021 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants