Skip to content

Commit

Permalink
Merge pull request #446 from vickymadrid03/refactor-slack-notifier-ca…
Browse files Browse the repository at this point in the history
…ll-method

Refactor call method from Slack Notifier
  • Loading branch information
FLarra committed Jan 23, 2019
2 parents 7d0d0d0 + 55da2d5 commit 0cf9f2e
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions lib/exception_notifier/slack_notifier.rb
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 0cf9f2e

Please sign in to comment.