Class: Rack::ExceptionNotifier
- Inherits:
-
Object
- Object
- Rack::ExceptionNotifier
- Defined in:
- lib/rack/exception_notifier.rb,
lib/rack/exception_notifier/version.rb
Constant Summary collapse
- ExcludeBodyKeys =
%w[rack.input rack.request.form_hash rack.request.form_input rack.request.form_vars]
- Template =
(<<-'EMAIL').gsub(/^ {4}/, '') A <%= exception.class.to_s %> occured: <%= exception.to_s %> <% if _render_body?(env) %> =================================================================== Request Body: =================================================================== <%= _extract_body(env).gsub(/^/, ' ') %> <% end %> =================================================================== Rack Environment: =================================================================== PID: <%= $$ %> PWD: <%= Dir.getwd %> <%= env.to_a. reject { |key, value| _exclude_env_key?(env, key) }. sort { |a, b| a.first <=> b.first }. map{ |key, value| "%-25s%p" % [key + ':', value] }. join("\n ") %> <% if exception.respond_to?(:backtrace) %> =================================================================== Backtrace: =================================================================== <%= exception.backtrace.join("\n ") %> <% end %> EMAIL
- VERSION =
'0.3.2'
Instance Method Summary collapse
- #_body_present?(env) ⇒ Boolean
- #_exclude_env_key?(env, key) ⇒ Boolean
- #_extract_body(env) ⇒ Object
- #_render_body?(env) ⇒ Boolean
- #_send_notification(exception, env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options) ⇒ ExceptionNotifier
constructor
A new instance of ExceptionNotifier.
Constructor Details
#initialize(app, options) ⇒ ExceptionNotifier
Returns a new instance of ExceptionNotifier.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rack/exception_notifier.rb', line 8 def initialize(app, ) = { :to => nil, :from => ENV['USER'] || 'rack@localhost', :subject => '[ERROR] %s', :include_body => false } @app = app @options = .merge() @template = ERB.new(Template) if @options[:to].nil? raise ArgumentError.new('to address is required') end end |
Instance Method Details
#_body_present?(env) ⇒ Boolean
41 42 43 |
# File 'lib/rack/exception_notifier.rb', line 41 def _body_present?(env) env['rack.input'].size > 0 end |
#_exclude_env_key?(env, key) ⇒ Boolean
45 46 47 48 49 50 51 |
# File 'lib/rack/exception_notifier.rb', line 45 def _exclude_env_key?(env, key) if _render_body?(env) false else ExcludeBodyKeys.include?(key) end end |
#_extract_body(env) ⇒ Object
57 58 59 60 61 |
# File 'lib/rack/exception_notifier.rb', line 57 def _extract_body(env) io = env['rack.input'] io.rewind if io.respond_to?(:rewind) io.read end |
#_render_body?(env) ⇒ Boolean
53 54 55 |
# File 'lib/rack/exception_notifier.rb', line 53 def _render_body?(env) _body_present?(env) && @options[:include_body] end |
#_send_notification(exception, env) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/rack/exception_notifier.rb', line 31 def _send_notification(exception, env) mail = Mail.new mail.to(@options[:to]) mail.reply_to(@options[:reply_to]) mail.from(@options[:from]) mail.subject(@options[:subject] % [exception.to_s]) mail.body(@template.result(binding)) mail.deliver! end |
#call(env) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rack/exception_notifier.rb', line 24 def call(env) @app.call(env) rescue => e _send_notification(e, env) raise end |