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 at_exit block automatically #397

Merged
merged 30 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3504423
Added configuration option for at_exit registration
Cawllec Nov 17, 2017
46e2b07
Changed behaviour not to add at_exit block automatically
Cawllec Nov 20, 2017
8eb7bcc
Added register_at_exit/add_exit_handler tests
Cawllec Nov 20, 2017
f1c1190
Change at_exit handler to opt-out
Cawllec Nov 21, 2017
63499c1
Added reset exit handler patch for testing
Cawllec Nov 22, 2017
0a45f26
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Jan 3, 2018
dca0537
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Feb 2, 2018
34f4b76
Made exit_handler off by default
Cawllec Feb 2, 2018
1b7822f
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Feb 8, 2018
9024774
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Feb 19, 2018
2985b0b
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Feb 20, 2018
16c572c
Reduced complexity
Cawllec Feb 20, 2018
0c42905
Reduce complexity again
Cawllec Feb 20, 2018
5e51c31
Separated warning function
Cawllec Feb 20, 2018
8dd01c7
Made add_exit_handler true by default
Cawllec Feb 20, 2018
0e0348f
Made at_exit enabled by default
Cawllec Feb 20, 2018
0b0e3b3
Removed superfluous config opt
Cawllec Feb 21, 2018
a119c30
Ammend spec to match new behaviour
Cawllec Feb 21, 2018
8da33d5
Moved test to relevant file, added integration test
Cawllec Feb 21, 2018
9515de8
Fixed outdated doc comment
Cawllec Feb 23, 2018
95dd9b3
Attempting to avoid at_exit stacks causing issues
Cawllec Feb 23, 2018
73e4156
Ammended spec helper to capture at_exit calls better
Cawllec Feb 23, 2018
8752319
Revert identation change
Cawllec Feb 23, 2018
ef71903
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Apr 10, 2018
dc27a28
Merge branch 'master' into cawllec/auto_at_exit
Cawllec Apr 25, 2018
c43785f
tests(Rack): Fix mocked rack constants
Cawllec May 2, 2018
b65d51b
Cleanup the tests and remove uncalled code
snmaynard May 11, 2018
53f8443
Merge branch 'master' into next
Cawllec May 11, 2018
cc25d59
Merge branch 'next' into cawllec/auto_at_exit
Cawllec May 11, 2018
3f70eef
feature(at_exit): Add at_exit block automatically
Cawllec May 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 111
Max: 125

# Offense count: 11
Metrics/PerceivedComplexity:
Expand Down
26 changes: 26 additions & 0 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def configure(validate_api_key=true)
yield(configuration) if block_given?

check_key_valid if validate_api_key

register_at_exit
end

##
Expand Down Expand Up @@ -111,6 +113,30 @@ def notify(exception, auto_notify=false, &block)
end
end

##
# Registers an at_exit function to automatically catch errors on exit
def register_at_exit
return if at_exit_handler_installed?
@exit_handler_added = true
at_exit do
if $!
Bugsnag.notify($!, true) do |report|
report.severity = 'error'
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
}
end
end
end
end

##
# Checks if an at_exit handler has been added
def at_exit_handler_installed?
@exit_handler_added ||= false
end

# Configuration getters
##
# Returns the client's Configuration object, or creates one if not yet created.
def configuration
Expand Down
17 changes: 0 additions & 17 deletions lib/bugsnag/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ class Railtie < Rails::Railtie
load "bugsnag/tasks/bugsnag.rake"
end

# send notifications if a command fails in a 'rails runner' call
if self.respond_to? :runner
runner do
at_exit do
if $!
Bugsnag.notify($!, true) do |report|
report.severity = "error"
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
:attributes => FRAMEWORK_ATTRIBUTES
}
end
end
end
end
end

config.before_initialize do
# Configure bugsnag rails defaults
# Skipping API key validation as the key may be set later in an
Expand Down
56 changes: 56 additions & 0 deletions spec/bugsnag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,62 @@
end
end

describe "add_exit_handler" do

before do
Bugsnag.instance_variable_set(:@exit_handler_added, false)
end

it "automatically adds an exit handler" do
expect(Bugsnag).to receive(:register_at_exit)
Bugsnag.configure do |conf|
conf.api_key = "TEST KEY"
end
end

it "calls at_exit when register_at_exit is called" do
expect(Bugsnag).to receive(:at_exit)
Bugsnag.register_at_exit
end

it "doesn't call at_exit on subsequent calls" do
expect(Bugsnag).to receive(:at_exit).once
Bugsnag.register_at_exit
Bugsnag.register_at_exit
end

context 'with aliased at_exit' do
before do
module Kernel
alias_method :old_at_exit, :at_exit
def at_exit
begin
raise BugsnagTestException.new("Oh no")
rescue
yield
end
end
end
end

it "sends an exception when at_exit is called" do
report_mock = double('report')
expect(report_mock).to receive(:severity=).with('error')
expect(report_mock).to receive(:severity_reason=).with({
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
})
expect(Bugsnag).to receive(:notify).with(kind_of(BugsnagTestException), true).and_yield(report_mock)
Bugsnag.register_at_exit
end

after do
module Kernel
alias_method :at_exit, :old_at_exit
end
end
end
end

describe 'loading integrations' do
before do
module Kernel
Expand Down
1 change: 1 addition & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,5 @@ def debug(name, &block)
it "should have exit exception classes ignored by default" do
expect(subject.ignore_classes).to eq(Set.new([SystemExit, Interrupt]))
end

end
6 changes: 3 additions & 3 deletions spec/integrations/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
before do
unless defined?(::Rack)
@mocked_rack = true
class ::Rack
class ::Request
class Rack
class Request
end
end
end
Expand Down Expand Up @@ -94,7 +94,7 @@ class ::Request
:referer => "referer",
:fullpath => "/TEST_PATH"
)
expect(::Rack::Request).to receive(:new).with(rack_env).and_return(rack_request)
expect(Rack::Request).to receive(:new).with(rack_env).and_return(rack_request)

report = double("Bugsnag::Report")
allow(report).to receive(:request_data).and_return({
Expand Down