Skip to content

Commit

Permalink
Change format to slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
eldano committed Dec 4, 2015
1 parent 83a2b72 commit 132be9e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
57 changes: 27 additions & 30 deletions lib/exception_notifier/slack_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ def initialize(options)
end

def call(exception, options={})
message = "An exception occurred: '#{exception.message}'"
message += " on '#{exception.backtrace.first}'" if exception.backtrace
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"

message = enrich_message_with_data(message, options)
message = enrich_message_with_backtrace(message, exception) if exception.backtrace
clean_message = exception.message.gsub("`", "'")
fields = [ { title: 'Exception', value: clean_message} ]

@notifier.ping(message, @message_opts) if valid?
if exception.backtrace
formatted_backtrace = "```#{exception.backtrace.first(5).join("\n")}```"
fields.push({ title: 'Backtrace', value: formatted_backtrace })
end

unless data.empty?
deep_reject(data, @ignore_data_if) if @ignore_data_if.is_a?(Proc)
data_string = data.map{|k,v| "#{k}: #{v}"}.join("\n")
fields.push({ title: 'Data', value: "```#{data_string}```" })
end

attchs = [color: 'danger', text: text, fields: fields, mrkdwn_in: %w(text fields)]

@notifier.ping '', @message_opts.merge(attachments: attchs) if valid?
end

protected
Expand All @@ -32,35 +47,17 @@ def valid?
!@notifier.nil?
end

def enrich_message_with_data(message, options)
def deep_reject(hash, block)
hash.each do |k, v|
if v.is_a?(Hash)
deep_reject(v, block)
end

if block.call(k, v)
hash.delete(k)
end
def deep_reject(hash, block)
hash.each do |k, v|
if v.is_a?(Hash)
deep_reject(v, block)
end
end

data = ((options[:env] || {})['exception_notifier.exception_data'] || {}).merge(options[:data] || {})
deep_reject(data, @ignore_data_if) if @ignore_data_if.is_a?(Proc)
text = data.map{|k,v| "#{k}: #{v}"}.join(', ')

unless text.nil? || text.empty?
text = ['*Data:*', text].join("\n")
[message, text].join("\n")
else
message
if block.call(k, v)
hash.delete(k)
end
end
end

def enrich_message_with_backtrace(message, exception)
backtrace = clean_backtrace(exception).first(10).join("\n")
[message, ['*Backtrace:*', backtrace]].join("\n")
end

end
end
33 changes: 17 additions & 16 deletions test/exception_notifier/slack_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setup
webhook_url: "http://slack.webhook.url"
}

Slack::Notifier.any_instance.expects(:ping).with(fake_notification, {})
Slack::Notifier.any_instance.expects(:ping).with('', fake_notification)

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception)
Expand All @@ -25,7 +25,7 @@ def setup
webhook_url: "http://slack.webhook.url"
}

Slack::Notifier.any_instance.expects(:ping).with(fake_notification_without_backtrace, {})
Slack::Notifier.any_instance.expects(:ping).with('', fake_notification(fake_exception_without_backtrace))

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(fake_exception_without_backtrace)
Expand All @@ -37,7 +37,7 @@ def setup
channel: "channel"
}

Slack::Notifier.any_instance.expects(:ping).with(fake_notification, {})
Slack::Notifier.any_instance.expects(:ping).with('', fake_notification)

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception)
Expand All @@ -51,7 +51,7 @@ def setup
username: "username"
}

Slack::Notifier.any_instance.expects(:ping).with(fake_notification, {})
Slack::Notifier.any_instance.expects(:ping).with('', fake_notification)

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception)
Expand All @@ -69,7 +69,7 @@ def setup
}
}

Slack::Notifier.any_instance.expects(:ping).with(fake_notification, {icon_url: "icon"})
Slack::Notifier.any_instance.expects(:ping).with('', options[:additional_parameters].merge(fake_notification) )

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception)
Expand All @@ -81,7 +81,7 @@ def setup
slack_notifier = ExceptionNotifier::SlackNotifier.new(options)

assert_nil slack_notifier.notifier
assert_nil slack_notifier.call(fake_exception)
assert_nil slack_notifier.call(@exception)
end

test "should pass along environment data" do
Expand All @@ -103,9 +103,9 @@ def setup
}
}

expected_data_string = 'foo: bar, john: doe, user_id: 5'
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, expected_data_string))
slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception, notification_options)
end
Expand All @@ -120,17 +120,18 @@ def fake_exception
end
end

def fake_notification(exception=@exception, data_string=nil)
message = "An exception occurred: '#{exception.message}' on '#{exception.backtrace.first}'\n"
message += "*Data:*\n#{data_string}\n" unless data_string.nil?
message += "*Backtrace:*\n" + exception.backtrace.join("\n")
end

def fake_exception_without_backtrace
StandardError.new('my custom error')
end

def fake_notification_without_backtrace
message = "An exception occurred: '#{fake_exception_without_backtrace.message}'"
def fake_notification(exception=@exception, data_string=nil)
text = "*An exception occurred while doing*: ` <>`\n"

fields = [ { title: 'Exception', value: exception.message} ]
fields.push({ title: 'Backtrace', value: "```backtrace line 1\nbacktrace line 2```" }) if exception.backtrace
fields.push({ title: 'Data', value: "```#{data_string}```" }) if data_string

{ attachments: [ color: 'danger', text: text, fields: fields, mrkdwn_in: %w(text fields) ] }
end

end

0 comments on commit 132be9e

Please sign in to comment.