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

Added 'did you mean' suggestions middleware #372

Merged
merged 6 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "bugsnag/middleware/callbacks"
require "bugsnag/middleware/exception_meta_data"
require "bugsnag/middleware/ignore_error_class"
require "bugsnag/middleware/suggestion_data"

module Bugsnag
class Configuration
Expand Down Expand Up @@ -73,6 +74,7 @@ def initialize
self.internal_middleware = Bugsnag::MiddlewareStack.new
self.internal_middleware.use Bugsnag::Middleware::ExceptionMetaData
self.internal_middleware.use Bugsnag::Middleware::IgnoreErrorClass
self.internal_middleware.use Bugsnag::Middleware::SuggestionData

self.middleware = Bugsnag::MiddlewareStack.new
self.middleware.use Bugsnag::Middleware::Callbacks
Expand Down
30 changes: 30 additions & 0 deletions lib/bugsnag/middleware/suggestion_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Bugsnag::Middleware
class SuggestionData

CAPTURE_REGEX = /Did you mean\?([\s\S]+)$/
DELIMITER = "\n"

def initialize(bugsnag)
@bugsnag = bugsnag
end

def call(report)
matches = []
report.raw_exceptions.each do |exception|
match = CAPTURE_REGEX.match(exception.message)
next unless match

suggestions = match.captures[0].split(DELIMITER)
matches.concat suggestions.map{ |suggestion| suggestion.strip }
end

if matches.size == 1
report.add_tab(:"did you mean", {:suggestion => matches.first})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commonly in other notifiers we use an error tab i believe to generically have information about the error? Having a tab with one entry in it doesnt seem like something we would want to do long term

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. We could use "Error" as the tab name, in case there is other error-specific items to add later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an idea I just had - its where the bugsnag_meta_data attached to the error could go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that risk making the 'custom' tab redundant? It might be useful creating an 'error' tab and renaming the 'custom' tab to separate error generated vs user generated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont understand the concern? The custom tab only shows up if someone puts something there. I was just saying if you attach metadata to the error and there was an error tab I would expect it to show up there.

Not a big deal though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cawllec The custom tab is where metadata is collated if it does not conform to the expected format (e.g., not nested so that there is a tab name and a key name, like if you use report.add_tab(:error, "foo")). Its a bit out of scope of this changeset, but I think I see what you mean. Let's use :error as the tab name here though, and then we could separately use it as the tab for bugsnag_meta_data in a later feature.

elsif matches.size > 1
report.add_tab(:"did you mean", {:suggestions => matches})
end

@bugsnag.call(report)
end
end
end
34 changes: 34 additions & 0 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,38 @@ def call(report)
}
end

if ruby_version_greater_equal?("2.3.0")
context "with a ruby version >= 2.3.0" do
it "attaches did you mean metadata when necessary" do
begin
"Test".starts_with? "T"
rescue Exception => e
Bugsnag.notify(e)
end

expect(Bugsnag).to have_sent_notification{ |payload|
event = get_event_from_payload(payload)
expect(event["metaData"]["did you mean"]).to_not be_nil
expect(event["metaData"]["did you mean"]).to eq({"suggestion" => "start_with?"})
}
end
end
end

context "with a ruby version < 2.3.0" do
if !ruby_version_greater_equal?("2.3.0")
it "doesn't attach did you mean metadata" do
begin
"Test".starts_with? "T"
rescue Exception => e
Bugsnag.notify(e)
end

expect(Bugsnag).to have_sent_notification{ |payload|
event = get_event_from_payload(payload)
expect(event["metaData"]["did you mean"]).to be_nil
}
end
end
end
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def notify_test_exception(*args)
Bugsnag.notify(RuntimeError.new("test message"), *args)
end

def ruby_version_greater_equal?(version)
current_version = RUBY_VERSION.split "."
target_version = version.split "."
(Integer(current_version[0]) >= Integer(target_version[0])) &&
(Integer(current_version[1]) >= Integer(target_version[1])) &&
(Integer(current_version[2]) >= Integer(target_version[2]))
end

RSpec.configure do |config|
config.order = "random"

Expand Down