diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index c47ddfa9..34e5591c 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ +* enhancements + * Added `clean_backtrace` option to SlackNotifier (defaults to true). + == 4.4.3 * big fixes diff --git a/docs/notifiers/slack.md b/docs/notifiers/slack.md index 3fa47be6..7eb3999c 100644 --- a/docs/notifiers/slack.md +++ b/docs/notifiers/slack.md @@ -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* diff --git a/lib/exception_notifier/slack_notifier.rb b/lib/exception_notifier/slack_notifier.rb index cf3ffed6..cf60fcd6 100644 --- a/lib/exception_notifier/slack_notifier.rb +++ b/lib/exception_notifier/slack_notifier.rb @@ -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) @@ -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]] diff --git a/test/exception_notifier/slack_notifier_test.rb b/test/exception_notifier/slack_notifier_test.rb index b8aeeaf7..9eeff9c5 100644 --- a/test/exception_notifier/slack_notifier_test.rb +++ b/test/exception_notifier/slack_notifier_test.rb @@ -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 = { @@ -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? @@ -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