From bc34548039fc05b5347b4857dcaa3b34a66694d3 Mon Sep 17 00:00:00 2001 From: Pablo Vizcay Date: Tue, 17 Nov 2020 11:26:08 -0300 Subject: [PATCH 1/3] added option clean_backtrace with default value true to SlackNotifier --- lib/exception_notifier/slack_notifier.rb | 5 ++++- test/exception_notifier/slack_notifier_test.rb | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) 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..83d71540 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 withouth 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 From d157bc494329a28097561a8f3f57b0b7ded0556f Mon Sep 17 00:00:00 2001 From: Pablo Vizcay Date: Tue, 17 Nov 2020 11:54:38 -0300 Subject: [PATCH 2/3] updated slack docs & CHANGELOG --- CHANGELOG.rdoc | 3 +++ docs/notifiers/slack.md | 6 ++++++ 2 files changed, 9 insertions(+) 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* From e27d60dc39ec03f55e4ff4e4e79b86f2420f5517 Mon Sep 17 00:00:00 2001 From: Pablo Vizcay Date: Tue, 17 Nov 2020 11:56:59 -0300 Subject: [PATCH 3/3] fixed typo --- test/exception_notifier/slack_notifier_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/exception_notifier/slack_notifier_test.rb b/test/exception_notifier/slack_notifier_test.rb index 83d71540..9eeff9c5 100644 --- a/test/exception_notifier/slack_notifier_test.rb +++ b/test/exception_notifier/slack_notifier_test.rb @@ -76,7 +76,7 @@ def setup slack_notifier.call(@exception) end - test 'should send the notification withouth cleaned backtrace lines if option is false' do + test 'should send the notification without cleaned backtrace lines if option is false' do options = { webhook_url: 'http://slack.webhook.url', clean_backtrace: false