Add singleton class extension support to Bugsnag::Middleware::ExceptionMetaData #426
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using
Bugsnag::MetaData
to attach metadata or other Bugsnag-specific data directly to an exception class works great for the common case:However, it doesn't support dynamically extending an exception class at runtime.
This used to work in Bugsnag 5.1.0 but broke when a type check was added in 6.0.0:
bugsnag-ruby/lib/bugsnag/middleware/exception_meta_data.rb
Line 10 in 94cea31
This pull request changes the implementation to use
respond_to?
over a type check. This supports the singleton class case above and any other case we've forgotten to handle. For example, a developer need not includeBugsnag::MetaData
directly, they need only to define thebugsnag_meta_data
method andBugsnag::Middleware::ExceptionMetaData
will pick up the data.This change also applies to
bugsnag_user_id
,bugsnag_context
, andbugsnag_grouping_hash
.Note: originally, I fixed my issue with a change to the type check –
exception.is_a?(Bugsnag::MetaData)
– which handles class and singleton class extension. After thinking on it for a bit and noticing that the code used to rely onrespond_to?
, I decided to go that route instead.