Skip to content

Commit

Permalink
Merge b3309e9 into c745491
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtad committed Mar 28, 2016
2 parents c745491 + b3309e9 commit 7b6d7af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
19 changes: 15 additions & 4 deletions lib/exception_notifier/slack_notifier.rb
Expand Up @@ -18,10 +18,21 @@ def initialize(options)
end

def call(exception, options={})
env = options[:env] || {}
title = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>"
data = (env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})
text = "*An exception occurred while doing*: `#{title}`\n"
exception_name = "*#{exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'}* `#{exception.class.to_s}`"

if options[:env].nil?
data = options[:data] || {}
text = "#{exception_name} *occured in background*\n"
else
env = options[:env]
data = (env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})

kontroller = env['action_controller.instance']
request = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>"
text = "#{exception_name} *occurred while* `#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>`"
text += " *was processed by* `#{kontroller.controller_name}##{kontroller.action_name}`" if kontroller
text += "\n"
end

clean_message = exception.message.gsub("`", "'")
fields = [ { title: 'Exception', value: clean_message} ]
Expand Down
19 changes: 16 additions & 3 deletions test/exception_notifier/slack_notifier_test.rb
Expand Up @@ -106,7 +106,7 @@ def setup

expected_data_string = "foo: bar\njohn: doe\nuser_id: 5"

Slack::Notifier.any_instance.expects(:ping).with('', fake_notification(@exception, expected_data_string))
Slack::Notifier.any_instance.expects(:ping).with('', fake_notification(@exception, notification_options, expected_data_string))
slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception, notification_options)
end
Expand Down Expand Up @@ -165,8 +165,21 @@ def fake_backtrace
]
end

def fake_notification(exception = @exception, data_string = nil)
text = "*An exception occurred while doing*: ` <>`\n"
def fake_notification(exception = @exception, notification_options = {}, data_string = nil)
exception_name = "*#{exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'}* `#{exception.class.to_s}`"
if notification_options[:env].nil?
text = "#{exception_name} *occured in background*"
else
env = notification_options[:env]

kontroller = env['action_controller.instance']
request = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>"

text = "#{exception_name} *occurred while* `#{request}`"
text += " *was processed by* `#{kontroller.controller_name}##{kontroller.action_name}`" if kontroller
end

text += "\n"

fields = [ { title: 'Exception', value: exception.message} ]
fields.push({ title: 'Hostname', value: 'example.com' })
Expand Down

0 comments on commit 7b6d7af

Please sign in to comment.