Class: Wrangler::ExceptionNotifier

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

Overview

handles notifying (sending emails) for the wrangler gem. configuration for the class is set in an initializer via the configure() method, e.g.

Wrangler::ExceptionNotifier.configure do |config|

config[:key] = value

end

see README or source code for possible values and default settings


Constant Summary collapse

@@smtp_settings_overrides =

smtp settings specific to this class; allows for changing settings (e.g. from-address) to be different from other emails sent in your app

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

Yields:



45
46
47
# File 'lib/wrangler/exception_notifier.rb', line 45

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

Instance Method Details

#ensure_session_loaded(request) ⇒ Object

helper to force loading of session data in case of lazy loading (Rails 2.3). if the argument isn’t a request object, then don’t bother, cause it won’t have session.




132
133
134
135
# File 'lib/wrangler/exception_notifier.rb', line 132

def ensure_session_loaded(request)
  request.session.inspect if !request.nil? && request.respond_to?(:session)
  true
end

#exception_notification(exception_classname, error_message, additional_messages, proc_name, backtrace, supplementary_info, status_code = nil, request_data = nil, request = nil) ⇒ Object

form and send the notification email (note: this gets called indirectly when you call ExceptionNotifier.deliver_exception_notification())

arguments:

- exception_classname: the class of exception that was raised, if any
- error_message: the short version of the error message to display
- additional_messages: a string or array of additional error messages
                       to be logged or sent in notification...allows
                       for more detail in case it won't all display
                       well in email subject line, for example
- proc_name: the name of the process in which the exception arised
- backtrace: the stack trace from the exception (passing in excplicitly
             because serializing the exception does not preserve the
             backtrace (in case delayed_job is used to async the email)
- supplementary_info: any other messages to be dumped into the
                      notification email. these are provided by the
                      client code via the :call_for_supplementary_info
                      option. can be a string or an array of strings.
- status_code: the (string) http status code that the exception has been
               mapped to. Optional, but no default is provided, so
               no status code info will be contained in the email.
- request_data: hash with relevant data from the request object.
                Optional, but if not present, then assumed the exception
                was not due to an http request and thus no request
                data will be contained in the email.
- request: the original request that resulted in the exception. may
           be nil and MUST be nil if calling this method with
           delayed_job. Optional.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wrangler/exception_notifier.rb', line 80

def exception_notification(exception_classname,
                           error_message,
                           additional_messages,
                           proc_name,
                           backtrace,
                           supplementary_info,
                           status_code = nil,
                           request_data = nil,
                           request = nil)

  # don't try to send email if there are no from or recipient addresses
  if config[:from_address].nil? ||
     config[:from_address].empty? ||
     config[:recipient_addresses].nil? ||
     config[:recipient_addresses].empty?

    return nil
  end

  ensure_session_loaded(request)

  # NOTE: be very careful pulling data out of request in the view...it is
  # NOT cleaned, and may contain private data (e.g. passwords), so
  # scrutinize any use of @request in the views!

  body_hash =
    { :exception_classname =>    exception_classname,
      :error_message => error_message,
      :additional_messages => additional_messages,
      :backtrace =>    backtrace,
      :supplementary_info => supplementary_info,
      :status_code =>  status_code,
      :request_data => request_data,
      :request =>      request
    }

  body_hash.merge! extract_data_from_request_data(request_data)
  from         config[:from_address]
  recipients   config[:recipient_addresses]
  subject      "[#{proc_name + (proc_name ? ' ' : '')}" +
               "#{config[:subject_prefix]}] " +
               "#{exception_classname || 'error'}: " +
               "#{error_message}"
  body         body_hash
  sent_on      Time.now
  content_type 'text/plain'
end

#extract_data_from_request_data(request_data) ⇒ Object

extract relevant (and serializable) data from a request object




139
140
141
142
143
144
145
146
147
148
# File 'lib/wrangler/exception_notifier.rb', line 139

def extract_data_from_request_data(request_data)
  if request_data
    { :host => host_from_request_data(request_data),
      :protocol => protocol_from_request_data(request_data),
      :uri => uri_from_request_data(request_data)
    }
  else
    {}
  end
end

#host_from_request_data(request_data) ⇒ Object

extract the host from request object




152
153
154
155
156
# File 'lib/wrangler/exception_notifier.rb', line 152

def host_from_request_data(request_data)
  request_data['HTTP_X_REAL_IP'] ||
    request_data['HTTP_X_FORWARDED_HOST'] ||
    request_data['HTTP_HOST']
end

#protocol_from_request_data(request_data) ⇒ Object

extract protocol from request object




160
161
162
# File 'lib/wrangler/exception_notifier.rb', line 160

def protocol_from_request_data(request_data)
  request_data['SERVER_PROTOCOL']
end

#smtp_settingsObject

Allows overriding smtp_settings without changing parent class’ settings. rails expects an instance method




21
22
23
# File 'lib/wrangler/exception_notifier.rb', line 21

def smtp_settings
  @@smtp_settings_overrides.reverse_merge @@smtp_settings
end

#uri_from_request_data(request_data) ⇒ Object

extract URI from request object




166
167
168
# File 'lib/wrangler/exception_notifier.rb', line 166

def uri_from_request_data(request_data)
  request_data['REQUEST_URI']
end