Report simple values easily

This commit is contained in:
Maikel Linke
2025-02-11 15:36:07 +11:00
parent fa7edbb073
commit 079d09b8b8
2 changed files with 13 additions and 0 deletions

View File

@@ -22,11 +22,16 @@ class Alert
# )
def self.raise(error, metadata = {}, &block)
Bugsnag.notify(error) do |payload|
unless metadata.respond_to?(:each)
metadata = { metadata: { data: metadata } }
end
metadata.each do |name, data|
# Bugsnag only reports metadata when given a Hash.
data = { data: } unless data.is_a?(Hash)
payload.add_metadata(name, data)
end
block.call(payload)
end
end

View File

@@ -49,6 +49,14 @@ RSpec.describe Alert do
Alert.raise("hey", data: "ABC123")
end
it "adds simple values as context" do
expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:metadata, { data: "ABC123" }
)
Alert.raise("hey", "ABC123")
end
it "is compatible with Bugsnag API" do
expect_any_instance_of(Bugsnag::Report).to receive(:add_metadata).with(
:order, { number: "ABC123" }