Class: AutoError::AppError

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/auto_error/app_error.rb

Class Method Summary collapse

Class Method Details

.add_context(env, opts) ⇒ Object



35
36
37
# File 'app/models/auto_error/app_error.rb', line 35

def add_context( env, opts )
  context(env).merge!( opts )
end

.clean_backtrace(backtrace) ⇒ Object



63
64
65
66
# File 'app/models/auto_error/app_error.rb', line 63

def self.clean_backtrace( backtrace )
  return backtrace unless Rails.respond_to?( :backtrace_cleaner )
  Rails.backtrace_cleaner.send( :filter, backtrace )
end

.clear_context!(env) ⇒ Object



42
43
44
# File 'app/models/auto_error/app_error.rb', line 42

def clear_context!( env )
  env['auto_error.app_error.context'] = {}
end

.context(env) ⇒ Object



38
39
40
41
# File 'app/models/auto_error/app_error.rb', line 38

def context( env )
  env['auto_error.app_error.context'] ||= {}
  env['auto_error.app_error.context']
end

.log!(env, exception, opts, data = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/auto_error/app_error.rb', line 11

def self.log!( env, exception, opts, data={} )
  opts[:data] = data
  opts[:data].merge! self.context(env)
  opts.merge!( {
    klass: exception.class.name,
    message: exception.message,
    backtrace: (
      exception.backtrace ? clean_backtrace(exception.backtrace) : []
    ).join("\n")
  } )
  app_error = create!( opts )

  if AutoError::Config.email_on_error.any?
    begin
      send_email!( env, exception, opts[:data] )
    rescue
      $stderr.puts "AutoError failed to send exception notification email to #{AutoError::Config.email_on_error.inspect} -- #{$!.inspect}"
    end
  end

  app_error
end

.send_email!(env, exception, data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/auto_error/app_error.rb', line 49

def self.send_email!( env, exception, data )
  options = env['exception_notifier.options'] || {}
  options[:ignore_exceptions] ||= ExceptionNotifier.default_ignore_exceptions
  options[:email_prefix] ||= "[#{Rails.application.class.name.split('::').first} ERROR] "
  options[:exception_recipients] = AutoError::Config.email_on_error
  options[:sender_address] = AutoError::Config.email_sender
  options[:data] = data

  unless Array.wrap(options[:ignore_exceptions]).include?( exception.class )
    ExceptionNotifier::Notifier.exception_notification( env, exception, options ).deliver
    env['exception_notifier.delivered'] = true
  end
end