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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
### New features

- [#113](https://github.com/rootstrap/exception_hunter/pull/113) Add configuration to turn on/off async logging using ActionJob. (
[@matteo95g][])
[@matteo95g][])
- [#119](https://github.com/rootstrap/exception_hunter/pull/119) Add support for tracking manual exceptions within transactions. ([@lalopsb][])

### Bug fixes

Expand Down
20 changes: 19 additions & 1 deletion lib/exception_hunter/tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ module Tracking
# @param [User] user in the current session. (optional)
# @return [void]
def track(exception, custom_data: {}, user: nil)
open_transactions? ? create_error_within_new_thread(exception, custom_data, user) : create_error(exception, custom_data, user)

nil
end

private

def create_error_within_new_thread(exception, custom_data, user)
Thread.new do
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?

end

def create_error(exception, custom_data, user)
ErrorCreator.call(
tag: ErrorCreator::MANUAL_TAG,
class_name: exception.class.to_s,
Expand All @@ -28,8 +44,10 @@ def track(exception, custom_data: {}, user: nil)
user: user,
environment_data: {}
)
end

nil
def open_transactions?
ActiveRecord::Base.connection.open_transactions.positive?
end
end
end
20 changes: 20 additions & 0 deletions spec/exception_hunter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module ExceptionHunter
let(:user) { OpenStruct.new(id: 3, email: 'example@example.com', name: 'John') }

context 'when tracking is enabled' do
before { allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(0) }

let(:error) { Error.last }

it 'creates a new error' do
Expand Down Expand Up @@ -55,6 +57,24 @@ module ExceptionHunter

expect(error.error_group.tags).to eq(['Manual'])
end

it ' does not create a new thread' do
expect(Thread).to_not receive(:new)

subject
end

context 'when the error is tracked within a transaction' do
before do
allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(1)
allow(Thread).to receive(:new).and_yield
end

it 'creates a new error within a new thread' do
expect { subject }.to change(Error, :count).by(1)
expect(Thread).to have_received(:new)
end
end
end

context 'when tracking is disabled' do
Expand Down