class ExceptionNotifier::DetailedSlackNotifier::DetailedSlackNotification

this class builds Slack exception notifications

Attributes

data[R]
env[R]
exception[R]
timestamp[R]

Public Class Methods

new(exception, options = {}) click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 27
def initialize(exception, options = {})
  @exception = exception
  @env = options[:env]
  @data = ((env && env['exception_notifier.exception_data']) || {}).merge(options[:data] || {})
  @timestamp = Time.zone.now
end

Public Instance Methods

attachments() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 41
def attachments
  array = []

  unless background_exception?
    array << session_attachment
    array << request_attachment
  end

  array << backtrace_attachment
  array << data_attachment
end
message() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 34
def message
  msg = "#{exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'} #{exception.class} occurred"
  msg << "#{background_exception? ? ' in background' : ''} at #{timestamp} :\n"
  msg << "#{exception.message}\n"
  msg
end

Private Instance Methods

background_exception?() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 126
def background_exception?
  env.nil?
end
backtrace_attachment() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 104
def backtrace_attachment
  {
    fallback: 'Backtrace',
    title: 'Backtrace',
    text: (exception.backtrace || []).join("\n"),
    color: '#3399ff'
  }
end
data_attachment() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 113
def data_attachment
  {
    fallback: 'Data',
    title: 'Data',
    text: JSON.pretty_generate(data),
    color: '#33aa55'
  }
end
request() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 57
def request
  @request ||= ActionDispatch::Request.new(env)
end
request_attachment() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 82
def request_attachment
  {
    fallback: 'Request',
    title: 'Request',
    text: text_from_hash(request_description),
    color: '#ff3399'
  }
end
request_description() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 91
def request_description
  {
    'URL' => request.url,
    'HTTP Method' => request.request_method,
    'IP address' => request.remote_ip,
    'Parameters' => request.filtered_parameters.inspect,
    'Timestamp' => timestamp.getutc,
    'Server' => Socket.gethostname,
    'Rails root' => Rails.root,
    'Process' => $PROCESS_ID
  }
end
session_attachment() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 70
def session_attachment
  {
    fallback: 'Session',
    title: 'Session',
    text: text_from_hash(
      'Session ID' => session_id,
      'Data' => JSON.pretty_generate(request.session.to_hash)
    ),
    color: '#444444'
  }
end
session_id() click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 61
def session_id
  id = '[FILTERED]'
  if request.ssl?
    id = request.session['session_id']
    id ||= (request.env['rack.session.options'] && request.env['rack.session.options'][:id]).inspect
  end
  id
end
text_from_hash(hash) click to toggle source
# File lib/exception_notifier/detailed_slack_notifier.rb, line 122
def text_from_hash(hash)
  hash.map { |k, v| "#{k}: #{v}" }.join("\n")
end