Skip to content

Commit

Permalink
fix(notify): Handle 'nil' being sent as an exception (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec authored and kattrali committed Apr 18, 2018
1 parent 9f91115 commit c05e4da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module Bugsnag
LOCK = Mutex.new
INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que]

NIL_EXCEPTION_DESCRIPTION = "'nil' was notified as an exception"

class << self
##
# Configure the Bugsnag notifier application-wide settings.
Expand All @@ -53,25 +55,9 @@ def notify(exception, auto_notify=false, &block)
auto_notify = false
end

if !configuration.auto_notify && auto_notify
configuration.debug("Not notifying because auto_notify is disabled")
return
end

if !configuration.valid_api_key?
configuration.debug("Not notifying due to an invalid api_key")
return
end
return unless deliver_notification?(exception, auto_notify)

if !configuration.should_notify_release_stage?
configuration.debug("Not notifying due to notify_release_stages :#{configuration.notify_release_stages.inspect}")
return
end

if exception.respond_to?(:skip_bugsnag) && exception.skip_bugsnag
configuration.debug("Not notifying due to skip_bugsnag flag")
return
end
exception = NIL_EXCEPTION_DESCRIPTION if exception.nil?

report = Report.new(exception, configuration, auto_notify)

Expand Down Expand Up @@ -176,6 +162,26 @@ def load_integration(integration)
end
end

private

def deliver_notification?(exception, auto_notify)
reason = abort_reason(exception, auto_notify)
configuration.debug(reason) unless reason.nil?
reason.nil?
end

def abort_reason(exception, auto_notify)
if !configuration.auto_notify && auto_notify
"Not notifying because auto_notify is disabled"
elsif !configuration.valid_api_key?
"Not notifying due to an invalid api_key"
elsif !configuration.should_notify_release_stage?
"Not notifying due to notify_release_stages :#{configuration.notify_release_stages.inspect}"
elsif exception.respond_to?(:skip_bugsnag) && exception.skip_bugsnag
"Not notifying due to skip_bugsnag flag"
end
end

# Check if the API key is valid and warn (once) if it is not
def check_key_valid
@key_warning = false unless defined?(@key_warning)
Expand Down
15 changes: 15 additions & 0 deletions spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,21 @@ def gloops
expect(Bugsnag).not_to have_sent_notification
end

it 'uses an appropriate message if nil is notified' do
Bugsnag.notify(nil)
expect(Bugsnag).to have_sent_notification{ |payload, headers|
event = payload["events"][0]
exception = event["exceptions"][0]
expect(exception["errorClass"]).to eq("RuntimeError")
expect(exception["message"]).to eq("'nil' was notified as an exception")

stacktrace = exception["stacktrace"][0]
expect(stacktrace["lineNumber"]).to eq(1049)
expect(stacktrace["file"]).to end_with("spec/report_spec.rb")
expect(stacktrace["code"]["1048"]).to eq(" it 'uses an appropriate message if nil is notified' do")
expect(stacktrace["code"]["1049"]).to eq(" Bugsnag.notify(nil)")
}
end

if defined?(JRUBY_VERSION)

Expand Down

0 comments on commit c05e4da

Please sign in to comment.