Skip to content

Commit

Permalink
Add block variant of add_on_error
Browse files Browse the repository at this point in the history
Usage:

  Bugsnag.on_error do |event|
    event.add_metadata(:info, { a: 1 })
  end
  • Loading branch information
imjoehaines committed May 22, 2024
1 parent 0bd883c commit c76a82d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
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

0 comments on commit c76a82d

Please sign in to comment.