module Roda::RodaPlugins::ErrorEmail

  1. lib/roda/plugins/error_email.rb

The error_email plugin adds an error_email instance method that sends an email related to the exception. This is designed for use in combination with the error_handler plugin:

plugin :error_email, to: 'to@example.com', from: 'from@example.com'
plugin :error_handler do |e|
  error_email(e)
  'Internal Server Error'
end

It is similar to the error_mail plugin, except that it uses net/smtp directly instead of using the mail library. If you are not already using the mail library in your application, using error_email may be preferable over error_mail in terms of limiting the number of dependencies.

Options:

:filter

Callable called with the key and value for each parameter, environment variable, and session value. If it returns true, the value of the parameter is filtered in the email.

:from

The From address to use in the email (required)

:headers

A hash of additional headers to use in the email (default: empty hash)

:host

The SMTP server to use to send the email (default: localhost)

:prefix

A prefix to use in the email’s subject line (default: no prefix)

:to

The To address to use in the email (required)

The subject of the error email shows the exception class and message. The body of the error email shows the backtrace of the error and the request environment, as well the request params and session variables (if any). You can also call error_email with a plain string instead of an exception, in which case the string is used as the subject, and no backtrace is included.

Note that emailing on every error as shown above is only appropriate for low traffic web applications. For high traffic web applications, use an error reporting service instead of this plugin.

Methods

Public Class

  1. configure

Constants

DEFAULTS = { :filter=>lambda{|k,v| false}, :headers=>OPTS, :host=>'localhost', # simplecov:disable :emailer=>lambda{|h| Net::SMTP.start(h[:host]){|s| s.send_message(h[:message], h[:from], h[:to])}}, # simplecov:enable :default_headers=>lambda do |h, e| subject = if e.respond_to?(:message) "#{e.class}: #{e.message}" else e.to_s end {'From'=>h[:from], 'To'=>h[:to], 'Subject'=>"#{h[:prefix]}#{subject}"} end, :body=>lambda do |s, e| filter = s.class.error_email_opts[:filter] format = lambda do |h| h = h.map{|k, v| "#{k.inspect} => #{filter.call(k, v) ? 'FILTERED' : v.inspect}"} h.sort! h.join("\n") end begin params = s.request.params params = (format[params] unless params.empty?) rescue params = 'Invalid Parameters!' end message = String.new message << <  

Public Class methods

configure(app, opts=OPTS)
[show source]
    # File lib/roda/plugins/error_email.rb
114 def self.configure(app, opts=OPTS)
115   RodaPlugins.check_supported_opts("the error_email plugin", opts, [*DEFAULTS.keys, :prefix, :to, :from])
116 
117   email_opts = app.opts[:error_email] ||= DEFAULTS
118   email_opts = email_opts.merge(opts)
119   email_opts[:headers] = email_opts[:headers].dup.freeze
120   unless email_opts[:to] && email_opts[:from]
121     raise RodaError, "must provide :to and :from options to error_email plugin"
122   end
123   app.opts[:error_email] = email_opts.freeze
124 end