Class: Rack::MailExceptions

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/mailexceptions.rb

Overview

Catches all exceptions raised from the app it wraps and sends a useful email with the exception, stacktrace, and contents of the environment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) {|_self| ... } ⇒ MailExceptions

Returns a new instance of MailExceptions.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/contrib/mailexceptions.rb', line 13

def initialize(app)
  @app = app
  @config = {
    :to      => nil,
    :from    => ENV['USER'] || 'rack',
    :subject => '[exception] %s',
    :smtp    => {
      :server         => 'localhost',
      :domain         => 'localhost',
      :port           => 25,
      :authentication => :login,
      :user_name      => nil,
      :password       => nil
    }
  }
  @template = ERB.new(TEMPLATE)
  yield self if block_given?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/rack/contrib/mailexceptions.rb', line 11

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rack/contrib/mailexceptions.rb', line 32

def call(env)
  status, headers, body =
    begin
      @app.call(env)
    rescue => boom
      # TODO don't allow exceptions from send_notification to
      # propogate
      send_notification boom, env
      raise
    end
  send_notification env['mail.exception'], env if env['mail.exception']
  [status, headers, body]
end

#smtp(settings = {}) ⇒ Object



50
51
52
# File 'lib/rack/contrib/mailexceptions.rb', line 50

def smtp(settings={})
  @config[:smtp].merge! settings
end