Class: ExceptionNotifier

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/exception_notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.catch_all(verbose = false) ⇒ Object



90
91
92
93
# File 'lib/exception_notifier.rb', line 90

def self.catch_all(verbose = false)
  puts "[CATCH ALL INVOKED] #{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/500.html" if verbose
  "#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/500.html"
end

.configure_exception_notifier {|@@config| ... } ⇒ Object

Yields:

  • (@@config)


31
32
33
# File 'lib/exception_notifier.rb', line 31

def self.configure_exception_notifier(&block)
  yield @@config
end

.exception_to_filenames(exception) ⇒ Object

Returns an array of potential filenames to look for eg. For the Exception Class - SuperExceptionNotifier::CustomExceptionClasses::MethodDisabled the filename handles are:

super_exception_notifier_custom_exception_classes_method_disabled
method_disabled


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/exception_notifier.rb', line 44

def self.exception_to_filenames(exception)
  filenames = []
  e = exception.to_s
  filenames << ExceptionNotifier.filenamify(e)

  last_colon = e.rindex(':')
  unless last_colon.nil?
    filenames << ExceptionNotifier.filenamify(e[(last_colon + 1)..(e.length - 1)])
  end
  filenames
end

.filenamify(str) ⇒ Object

Converts Stringified Class Names to acceptable filename handles with underscores



62
63
64
# File 'lib/exception_notifier.rb', line 62

def self.filenamify(str)
  str.delete(':').gsub( /([A-Za-z])([A-Z])/, '\1' << '_' << '\2').downcase
end

.get_view_path(file_name, verbose = false) ⇒ Object

Check the usual suspects



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/exception_notifier.rb', line 96

def self.get_view_path(file_name, verbose = false)
  if File.exist?("#{RAILS_ROOT}/public/#{file_name}.html")
    puts "[FOUND FILE] #{RAILS_ROOT}/public/#{file_name}.html" if verbose
    "#{RAILS_ROOT}/public/#{file_name}.html"
  elsif !config[:view_path].nil? && File.exist?("#{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html.erb")
    puts "[FOUND FILE] #{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html.erb" if verbose
    "#{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html.erb"
  elsif !config[:view_path].nil? && File.exist?("#{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html")
    puts "[FOUND FILE] #{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html" if verbose
    "#{RAILS_ROOT}/#{config[:view_path]}/#{file_name}.html"
  elsif File.exist?("#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html.erb")
    puts "[FOUND FILE] #{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html.erb" if verbose
    "#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html.erb"
  elsif File.exist?("#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html")
    #ExceptionNotifierHelper::COMPAT_MODE ? "#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html" : "#{status_cd}.html"
    puts "[FOUND FILE] #{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html" if verbose
    "#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/#{file_name}.html"
  else
    nil
  end
end

.get_view_path_for_class(exception, verbose = false) ⇒ Object

What is the path of the file we will render to the user based on a given exception class?



81
82
83
84
85
86
87
88
# File 'lib/exception_notifier.rb', line 81

def self.get_view_path_for_class(exception, verbose = false)
  return self.catch_all if exception.nil?
  return self.catch_all unless exception.is_a?(StandardError) || exception.is_a?(Class) # For some reason exception.is_a?(Class) works in console, but not when running in mongrel (ALWAYS returns false)?!?!?
  filepaths = ExceptionNotifier.exception_to_filenames(exception).map do |file|
    ExceptionNotifier.get_view_path(file, verbose)
  end.compact
  filepaths.empty? ? self.catch_all(verbose) : filepaths.first
end

.get_view_path_for_status_code(status_cd, verbose = false) ⇒ Object

What is the path of the file we will render to the user based on a given status code?



67
68
69
70
71
# File 'lib/exception_notifier.rb', line 67

def self.get_view_path_for_status_code(status_cd, verbose = false)
  file_name = ExceptionNotifier.get_view_path(status_cd, verbose)
  #ExceptionNotifierHelper::COMPAT_MODE ? "#{File.dirname(__FILE__)}/../rails/app/views/exception_notifiable/500.html" : "500.html"
  file_name.nil? ? self.catch_all(verbose) : file_name
end

.reloadable?Boolean

Returns:

  • (Boolean)


37
# File 'lib/exception_notifier.rb', line 37

def self.reloadable?() false end

.sections_for_email(rejected_sections, request) ⇒ Object



56
57
58
59
# File 'lib/exception_notifier.rb', line 56

def self.sections_for_email(rejected_sections, request)
  rejected_sections = rejected_sections.nil? ? request.nil? ? %w(request session) : [] : rejected_sections
  rejected_sections.empty? ? config[:sections] : config[:sections].reject{|s| rejected_sections.include?(s) }
end

Instance Method Details

#background_exception_notification(exception, data = {}, the_blamed = nil, rejected_sections = %w(request session))) ⇒ Object



131
132
133
# File 'lib/exception_notifier.rb', line 131

def background_exception_notification(exception, data = {}, the_blamed = nil, rejected_sections = %w(request session))
  exception_notification(exception, nil, nil, nil, data, the_blamed, rejected_sections)
end

#exception_notification(exception, class_name = nil, method_name = nil, request = nil, data = {}, the_blamed = nil, rejected_sections = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/exception_notifier.rb', line 118

def exception_notification(exception, class_name = nil, method_name = nil, request = nil, data = {}, the_blamed = nil, rejected_sections = nil)
  body_hash = error_environment_data_hash(exception, class_name, method_name, request, data, the_blamed, rejected_sections)
  #Prefer to have custom, potentially HTML email templates available
  #content_type  "text/plain"
  recipients    config[:exception_recipients]
  from          config[:sender_address]

  request.session.inspect unless request.nil? # Ensure session data is loaded (Rails 2.3 lazy-loading)
  
  subject       "#{config[:subject_prepend]}#{body_hash[:location]} (#{exception.class}) #{exception.message.inspect}#{config[:subject_append]}"
  body          body_hash
end

#rake_exception_notification(exception, task, data = {}, the_blamed = nil, rejected_sections = %w(request session))) ⇒ Object



135
136
137
# File 'lib/exception_notifier.rb', line 135

def rake_exception_notification(exception, task, data={}, the_blamed = nil, rejected_sections = %w(request session))
  exception_notification(exception, "", "#{task.name}", nil, data, the_blamed, rejected_sections)
end