lib/roda/plugins/error_mail.rb in roda-3.75.0 vs lib/roda/plugins/error_mail.rb in roda-3.76.0
- old
+ new
@@ -19,10 +19,13 @@
# mail library in your application, it makes sense to use error_mail
# instead of error_email.
#
# 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)
# :prefix :: A prefix to use in the email's subject line (default: no prefix)
# :to :: The To address to use in the email (required)
#
@@ -34,13 +37,16 @@
#
# 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.
module ErrorMail
+ DEFAULT_FILTER = lambda{|k,v| false}
+ private_constant :DEFAULT_FILTER
+
# Set default opts for plugin. See ErrorEmail module RDoc for options.
def self.configure(app, opts=OPTS)
- app.opts[:error_mail] = email_opts = (app.opts[:error_mail] || OPTS).merge(opts).freeze
+ app.opts[:error_mail] = email_opts = (app.opts[:error_mail] || {:filter=>DEFAULT_FILTER}).merge(opts).freeze
unless email_opts[:to] && email_opts[:from]
raise RodaError, "must provide :to and :from options to error_mail plugin"
end
end
@@ -66,11 +72,16 @@
"#{e.class}: #{e.message}"
else
e.to_s
end
subject = "#{email_opts[:prefix]}#{subject}"
+ filter = email_opts[:filter]
- format = lambda{|h| h.map{|k, v| "#{k.inspect} => #{v.inspect}"}.sort.join("\n")}
+ 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 = request.params
params = (format[params] unless params.empty?)
rescue