Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* enhancements
* Added `clean_backtrace` option to SlackNotifier (defaults to true).

== 4.4.3

* big fixes
Expand Down
6 changes: 6 additions & 0 deletions docs/notifiers/slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ Message will appear in this channel. Defaults to the channel you set as such on

Username of the bot. Defaults to the name you set as such on slack

##### clean_backtrace

*Boolean, optional*

If enabled will clean the exception backtrace with Rails.backtrace_cleaner. Defaults to true.

##### custom_hook

*String, optional*
Expand Down
5 changes: 4 additions & 1 deletion lib/exception_notifier/slack_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(options)
begin
@ignore_data_if = options[:ignore_data_if]
@backtrace_lines = options.fetch(:backtrace_lines, 10)
@clean_backtrace = options.delete(:clean_backtrace) { true }
@additional_fields = options[:additional_fields]

webhook_url = options.fetch(:webhook_url)
Expand Down Expand Up @@ -54,7 +55,9 @@ def deep_reject(hash, block)

def attchs(exception, clean_message, options)
text, data = information_from_options(exception.class, options)
backtrace = clean_backtrace(exception) if exception.backtrace
backtrace = if exception.backtrace
@clean_backtrace ? clean_backtrace(exception) : exception.backtrace
end
fields = fields(clean_message, backtrace, data)

[color: @color, text: text, fields: fields, mrkdwn_in: %w[text fields]]
Expand Down
18 changes: 16 additions & 2 deletions test/exception_notifier/slack_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ def setup
slack_notifier.call(@exception)
end

test 'should send the notification without cleaned backtrace lines if option is false' do
options = {
webhook_url: 'http://slack.webhook.url',
clean_backtrace: false
}

Slack::Notifier.any_instance.expects(:ping).with('', fake_notification(@exception, {}, nil, 6, [], false))

slack_notifier = ExceptionNotifier::SlackNotifier.new(options)
slack_notifier.call(@exception)
end

test 'should send the notification with additional fields' do
field = { title: 'Branch', value: 'master', short: true }
options = {
Expand Down Expand Up @@ -198,7 +210,8 @@ def fake_cleaned_backtrace
end

def fake_notification(exception = @exception, notification_options = {},
data_string = nil, expected_backtrace_lines = 10, additional_fields = [])
data_string = nil, expected_backtrace_lines = 10,
additional_fields = [], clean_backtrace = true)

exception_name = "*#{exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'}* `#{exception.class}`"
if notification_options[:env].nil?
Expand All @@ -218,7 +231,8 @@ def fake_notification(exception = @exception, notification_options = {},
fields = [{ title: 'Exception', value: exception.message }]
fields.push(title: 'Hostname', value: 'example.com')
if exception.backtrace
formatted_backtrace = "```#{fake_cleaned_backtrace.first(expected_backtrace_lines).join("\n")}```"
backtrace = clean_backtrace ? fake_cleaned_backtrace : fake_backtrace
formatted_backtrace = "```#{backtrace.first(expected_backtrace_lines).join("\n")}```"
fields.push(title: 'Backtrace', value: formatted_backtrace)
end
fields.push(title: 'Data', value: "```#{data_string}```") if data_string
Expand Down