From 55da2d58864166c9494771c83165b626acdd911e Mon Sep 17 00:00:00 2001 From: Victoria Madrid Date: Wed, 19 Dec 2018 19:03:55 -0300 Subject: [PATCH] Refactor call method from Slack notifier --- lib/exception_notifier/slack_notifier.rb | 92 ++++++++++++++---------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/exception_notifier/slack_notifier.rb b/lib/exception_notifier/slack_notifier.rb index 6f6447e2..f3fbeefa 100644 --- a/lib/exception_notifier/slack_notifier.rb +++ b/lib/exception_notifier/slack_notifier.rb @@ -21,46 +21,12 @@ def initialize(options) end def call(exception, options={}) - errors_count = options[:accumulated_errors_count].to_i - measure_word = errors_count > 1 ? errors_count : (exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A') - exception_name = "*#{measure_word}* `#{exception.class.to_s}`" - - if options[:env].nil? - data = options[:data] || {} - text = "#{exception_name} *occured in background*\n" - else - env = options[:env] - data = (env['exception_notifier.exception_data'] || {}).merge(options[:data] || {}) - - kontroller = env['action_controller.instance'] - request = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>" - text = "#{exception_name} *occurred while* `#{request}`" - text += " *was processed by* `#{kontroller.controller_name}##{kontroller.action_name}`" if kontroller - text += "\n" - end - clean_message = exception.message.gsub("`", "'") - fields = [ { title: 'Exception', value: clean_message } ] - - fields.push({ title: 'Hostname', value: Socket.gethostname }) - - if exception.backtrace - formatted_backtrace = "```#{exception.backtrace.first(@backtrace_lines).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 - - fields.concat(@additional_fields) if @additional_fields - - attchs = [color: @color, text: text, fields: fields, mrkdwn_in: %w(text fields)] + attchs = attchs(exception, clean_message, options) if valid? - send_notice(exception, options, clean_message, @message_opts.merge(attachments: attchs)) do |msg, message_opts| + args = [exception, options, clean_message, @message_opts.merge(attachments: attchs)] + send_notice(*args) do |msg, message_opts| @notifier.ping '', message_opts end end @@ -84,5 +50,57 @@ def deep_reject(hash, block) end end + private + + def attchs(exception, clean_message, options) + text, data = information_from_options(exception.class, options) + fields = fields(clean_message, exception.backtrace, data) + + [color: @color, text: text, fields: fields, mrkdwn_in: %w(text fields)] + end + + def information_from_options(exception_class, options) + errors_count = options[:accumulated_errors_count].to_i + measure_word = errors_count > 1 ? errors_count : (exception_class.to_s =~ /^[aeiou]/i ? 'An' : 'A') + exception_name = "*#{measure_word}* `#{exception_class.to_s}`" + env = options[:env] + + if env.nil? + data = options[:data] || {} + text = "#{exception_name} *occured in background*\n" + else + data = (env['exception_notifier.exception_data'] || {}).merge(options[:data] || {}) + + kontroller = env['action_controller.instance'] + request = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>" + text = "#{exception_name} *occurred while* `#{request}`" + text += " *was processed by* `#{kontroller.controller_name}##{kontroller.action_name}`" if kontroller + text += "\n" + end + + [text, data] + end + + def fields(clean_message, backtrace, data) + fields = [ + { title: 'Exception', value: clean_message }, + { title: 'Hostname', value: Socket.gethostname } + ] + + if backtrace + formatted_backtrace = "```#{backtrace.first(@backtrace_lines).join("\n")}```" + fields << { 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 << { title: 'Data', value: "```#{data_string}```" } + end + + fields.concat(@additional_fields) if @additional_fields + + fields + end end end