Class: Rack::EmailExceptions

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

Instance Method Summary collapse

Constructor Details

#initialize(app, prefix, email) ⇒ EmailExceptions

Store the prefix to use in the email in addition to the next application.



11
12
13
14
15
# File 'lib/rack/email_exceptions.rb', line 11

def initialize(app, prefix, email)
  @app = app
  @prefix = prefix
  @email = email
end

Instance Method Details

#call(env) ⇒ Object

Rescue any errors raised by calling the next application, and if there is an error, email about it before reraising it.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/email_exceptions.rb', line 19

def call(env)
  @app.call(env)
rescue StandardError, ScriptError => e
  body = <<END
From: #{@email}\r
To: #{@email}\r
Subject: [#{@prefix}] Unhandled Error Raised by Rack App or Middleware\r
\r
\r
Error: #{e.class}: #{e.message}

Backtrace:

#{e.backtrace.join("\n")}

ENV:

#{env.map{|k, v| "#{k.inspect} => #{v.inspect}"}.sort.join("\n")}
END

  # :nocov:
  # Don't verify localhost hostname, to avoid SSL errors raised in newer versions of net/smtp
  smtp_params = Net::SMTP.method(:start).parameters.include?([:key, :tls_verify]) ? {tls_verify: false, tls_hostname: 'localhost'} : {}
  # :nocov:

  Net::SMTP.start('127.0.0.1', **smtp_params){|s| s.send_message(body, @email, @email)}

  raise
end