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 block variant of add_on_error #824

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
14 changes: 14 additions & 0 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
39 changes: 39 additions & 0 deletions spec/on_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,30 @@
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" }) }

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!"))
Expand All @@ -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

Expand All @@ -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" }) }
Expand Down
Loading