mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Bugsnag expects a string as value, not an ActiveRecord object. The result was just "filtered" data, not showing any of the order's attributes. Since wanting to share the order data seems a common pattern, I added a convenience method for it.
39 lines
1.1 KiB
Ruby
39 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# A handy wrapper around error reporting libraries like Bugsnag.
|
|
#
|
|
# Bugsnag's API is great for general purpose but overly complex for our use.
|
|
# It also changes over time and we often make mistakes using it. So this class
|
|
# aims at:
|
|
#
|
|
# * Abstracting from Bugsnag, open for other services.
|
|
# * Simpler interface to reduce user error.
|
|
# * Central place to update Bugsnag API usage when it changes.
|
|
#
|
|
class Alert
|
|
# Alert Bugsnag with additional metadata to appear in tabs.
|
|
#
|
|
# Alert.raise(
|
|
# "Invalid order during checkout",
|
|
# {
|
|
# order: { number: "ABC123", state: "awaiting_return" },
|
|
# env: { referer: "example.com" }
|
|
# }
|
|
# )
|
|
def self.raise(error, metadata = {}, &block)
|
|
Bugsnag.notify(error) do |payload|
|
|
metadata.each do |name, data|
|
|
payload.add_metadata(name, data)
|
|
end
|
|
block.call(payload)
|
|
end
|
|
end
|
|
|
|
def self.raise_with_record(error, record, &)
|
|
metadata = {
|
|
record.class.name => record&.attributes || { record_was_nil: true }
|
|
}
|
|
self.raise(error, metadata, &)
|
|
end
|
|
end
|