From 429e12b4b76ae86ee4ba837f6d78129569f6822a Mon Sep 17 00:00:00 2001 From: Romain Pomier Date: Sun, 20 Jan 2019 12:38:03 +0100 Subject: [PATCH] Use backtrace cleaner for Slack notifications --- lib/exception_notifier/slack_notifier.rb | 3 ++- test/exception_notifier/slack_notifier_test.rb | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/exception_notifier/slack_notifier.rb b/lib/exception_notifier/slack_notifier.rb index 6f6447e2..c66dc8d4 100644 --- a/lib/exception_notifier/slack_notifier.rb +++ b/lib/exception_notifier/slack_notifier.rb @@ -45,7 +45,8 @@ def call(exception, options={}) fields.push({ title: 'Hostname', value: Socket.gethostname }) if exception.backtrace - formatted_backtrace = "```#{exception.backtrace.first(@backtrace_lines).join("\n")}```" + backtrace = clean_backtrace(exception) + formatted_backtrace = "```#{backtrace.first(@backtrace_lines).join("\n")}```" fields.push({ title: 'Backtrace', value: formatted_backtrace }) end diff --git a/test/exception_notifier/slack_notifier_test.rb b/test/exception_notifier/slack_notifier_test.rb index a649076a..f2e510e2 100644 --- a/test/exception_notifier/slack_notifier_test.rb +++ b/test/exception_notifier/slack_notifier_test.rb @@ -7,6 +7,7 @@ def setup @exception = fake_exception @exception.stubs(:backtrace).returns(fake_backtrace) @exception.stubs(:message).returns('exception message') + ExceptionNotifier::SlackNotifier.any_instance.stubs(:clean_backtrace).returns(fake_cleaned_backtrace) Socket.stubs(:gethostname).returns('example.com') end @@ -195,6 +196,10 @@ def fake_backtrace ] end + def fake_cleaned_backtrace + fake_backtrace[2..-1] + end + def fake_notification(exception = @exception, notification_options = {}, data_string = nil, expected_backtrace_lines = 10, additional_fields = []) exception_name = "*#{exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'}* `#{exception.class.to_s}`" if notification_options[:env].nil? @@ -214,7 +219,7 @@ def fake_notification(exception = @exception, notification_options = {}, data_st fields = [ { title: 'Exception', value: exception.message} ] fields.push({ title: 'Hostname', value: 'example.com' }) if exception.backtrace - formatted_backtrace = "```#{exception.backtrace.first(expected_backtrace_lines).join("\n")}```" + formatted_backtrace = "```#{fake_cleaned_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