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



33
34
35
# File 'app/models/auto_error/app_error.rb', line 33

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

.clean_backtrace(backtrace) ⇒ Object



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

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

.clear_context!(env) ⇒ Object



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

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

.context(env) ⇒ Object



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

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

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



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

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



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

def self.send_email!( env, exception, data )
  options = env['exception_notifier.options'] || {}
  options[:ignore_exceptions] ||= ExceptionNotifier.ignored_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
  options[:env] = env

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