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 singleton class extension support to Bugsnag::Middleware::ExceptionMetaData #426

Merged
merged 1 commit into from
Feb 13, 2018
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
26 changes: 12 additions & 14 deletions lib/bugsnag/middleware/exception_meta_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ def initialize(bugsnag)
def call(report)
# Apply the user's information attached to the exceptions
report.raw_exceptions.each do |exception|
if exception.class.include?(Bugsnag::MetaData)
if exception.bugsnag_user_id.is_a?(String)
report.user = {id: exception.bugsnag_user_id}
end
if exception.respond_to?(:bugsnag_user_id) && exception.bugsnag_user_id.is_a?(String)
report.user = {id: exception.bugsnag_user_id}
end

if exception.bugsnag_context.is_a?(String)
report.context = exception.bugsnag_context
end
if exception.respond_to?(:bugsnag_context) && exception.bugsnag_context.is_a?(String)
report.context = exception.bugsnag_context
end

if exception.bugsnag_grouping_hash.is_a?(String)
report.grouping_hash = exception.bugsnag_grouping_hash
end
if exception.respond_to?(:bugsnag_grouping_hash) && exception.bugsnag_grouping_hash.is_a?(String)
report.grouping_hash = exception.bugsnag_grouping_hash
end

if exception.respond_to?(:bugsnag_meta_data) && exception.bugsnag_meta_data
exception.bugsnag_meta_data.each do |key, value|
report.add_tab key, value
end
if exception.respond_to?(:bugsnag_meta_data) && exception.bugsnag_meta_data
exception.bugsnag_meta_data.each do |key, value|
report.add_tab key, value
end
end
end
Expand Down
90 changes: 90 additions & 0 deletions spec/middleware/exception_meta_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# encoding: utf-8
require 'spec_helper'

describe Bugsnag::Middleware::ExceptionMetaData do
let(:report_class) do
Class.new do
attr_accessor :raw_exceptions, :tabs, :user, :context, :grouping_hash

def initialize(errors)
self.raw_exceptions = Array(errors)
end

def add_tab(key, value)
self.tabs ||= {}
tabs[key] = value
end
end
end

let(:middleware) { Bugsnag::Middleware::ExceptionMetaData.new(lambda {|_|}) }
let(:bugsnag_error_class) { Class.new(StandardError) { include Bugsnag::MetaData } }

it "adds metadata when exception singleton class extended with Bugsnag::MetaData" do
error = RuntimeError.new
error.extend(Bugsnag::MetaData)
error.bugsnag_meta_data = {"foo" => "bar"}

report = report_class.new(error)

middleware.call(report)

expect(report.tabs).to eq({"foo" => "bar"})
end

it "adds metadata when exception class includes Bugsnag::MetaData" do
error = bugsnag_error_class.new
error.bugsnag_meta_data = {"foo" => "bar"}

report = report_class.new(error)

middleware.call(report)

expect(report.tabs).to eq({"foo" => "bar"})
end

it "sets user ID when a string" do
error = bugsnag_error_class.new
error.bugsnag_user_id = "1234"

report = report_class.new(error)

middleware.call(report)

expect(report.user).to eq({id: "1234"})
end

it "sets context when a string" do
error = bugsnag_error_class.new
error.bugsnag_context = "Foo#bar"

report = report_class.new(error)

middleware.call(report)

expect(report.context).to eq("Foo#bar")
end

it "sets grouping_hash when a string" do
error = bugsnag_error_class.new
error.bugsnag_grouping_hash = "abcdef"

report = report_class.new(error)

middleware.call(report)

expect(report.grouping_hash).to eq("abcdef")
end

it "does nothing when no bugsnag attributes are set" do
error = bugsnag_error_class.new
report = report_class.new(error)

middleware.call(report)

expect(report.user).to eq(nil)
expect(report.tabs).to eq(nil)
expect(report.grouping_hash).to eq(nil)
expect(report.context).to eq(nil)
end
end