From 0e7caef462c9bb23eecb4d27e193b475b225911e Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 22 May 2024 09:16:07 +0100 Subject: [PATCH] Add block variant of `add_on_error` Usage: Bugsnag.on_error do |event| event.add_metadata(:info, { a: 1 }) end --- CHANGELOG.md | 2 ++ lib/bugsnag.rb | 14 +++++++++++++ lib/bugsnag/configuration.rb | 14 +++++++++++++ spec/on_error_spec.rb | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d17bd5f6..dca836b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Changelog * Include the Warden scope in user metadata | [#821](https://github.com/bugsnag/bugsnag-ruby/pull/821) | [javierjulio](https://github.com/javierjulio) +* Add a block variant of `add_on_error` + | [#824](https://github.com/bugsnag/bugsnag-ruby/pull/824) ## v6.26.4 (25 March 2024) diff --git a/lib/bugsnag.rb b/lib/bugsnag.rb index 5d002a90..950f1726 100644 --- a/lib/bugsnag.rb +++ b/lib/bugsnag.rb @@ -309,6 +309,20 @@ def leave_breadcrumb(name, meta_data={}, type=Bugsnag::Breadcrumbs::MANUAL_BREAD configuration.breadcrumbs << breadcrumb unless breadcrumb.ignore? end + ## + # Add the given block to the list of on_error callbacks + # + # The on_error callbacks will be called when an error is captured or reported + # and are passed a {Bugsnag::Report} object + # + # Returning false from an on_error callback will cause the error to be ignored + # and will prevent any remaining callbacks from being called + # + # @return [void] + def on_error(&block) + configuration.on_error(&block) + end + ## # Add the given callback to the list of on_error callbacks # diff --git a/lib/bugsnag/configuration.rb b/lib/bugsnag/configuration.rb index 36dda53d..bcf64c81 100644 --- a/lib/bugsnag/configuration.rb +++ b/lib/bugsnag/configuration.rb @@ -569,6 +569,20 @@ def disable_sessions @enable_sessions = false end + ## + # Add the given block to the list of on_error callbacks + # + # The on_error callbacks will be called when an error is captured or reported + # and are passed a {Bugsnag::Report} object + # + # Returning false from an on_error callback will cause the error to be ignored + # and will prevent any remaining callbacks from being called + # + # @return [void] + def on_error(&block) + middleware.use(block) + end + ## # Add the given callback to the list of on_error callbacks # diff --git a/spec/on_error_spec.rb b/spec/on_error_spec.rb index 15bcdb5a..798befd1 100644 --- a/spec/on_error_spec.rb +++ b/spec/on_error_spec.rb @@ -18,6 +18,20 @@ end) end + it "accepts a block" do + Bugsnag.on_error {|report| report.add_tab(:important, { hello: "world" }) } + Bugsnag.on_error {|report| report.add_tab(:significant, { hey: "earth" }) } + + Bugsnag.notify(RuntimeError.new("Oh no!")) + + expect(Bugsnag).to(have_sent_notification do |payload, _headers| + event = get_event_from_payload(payload) + + expect(event["metaData"]["important"]).to eq({ "hello" => "world" }) + expect(event["metaData"]["significant"]).to eq({ "hey" => "earth" }) + end) + end + it "can add callbacks in a configure block" do callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) } callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) } @@ -25,6 +39,9 @@ Bugsnag.configure do |config| config.add_on_error(callback1) config.add_on_error(callback2) + config.on_error do |report| + report.add_tab(:critical, { hi: "planet" }) + end end Bugsnag.notify(RuntimeError.new("Oh no!")) @@ -34,6 +51,7 @@ expect(event["metaData"]["important"]).to eq({ "hello" => "world" }) expect(event["metaData"]["significant"]).to eq({ "hey" => "earth" }) + expect(event["metaData"]["critical"]).to eq({ "hi" => "planet" }) end) end @@ -56,6 +74,27 @@ end) end + it "can remove an already registered block" do + callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) } + callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) } + + Bugsnag.add_on_error(callback1) + + # pass callback2 as a block so that it can be removed + Bugsnag.on_error(&callback2) + + Bugsnag.remove_on_error(callback2) + + Bugsnag.notify(RuntimeError.new("Oh no!")) + + expect(Bugsnag).to(have_sent_notification do |payload, _headers| + event = get_event_from_payload(payload) + + expect(event["metaData"]["important"]).to eq({ "hello" => "world" }) + expect(event["metaData"]["significant"]).to be_nil + end) + end + it "can remove all registered callbacks" do callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) } callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) }