Class: Utopia::Exceptions::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/exceptions/mailer.rb

Overview

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

Constant Summary collapse

LOCAL_SMTP =

A basic local non-authenticated SMTP server.

[:smtp, {
	:address => "localhost",
	:port => 25,
	:enable_starttls_auto => false
}]
DEFAULT_FROM =
(ENV['USER'] || 'utopia').freeze
DEFAULT_SUBJECT =
'%{exception} [PID %{pid} : %{cwd}]'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, to: "postmaster", from: DEFAULT_FROM, subject: DEFAULT_SUBJECT, delivery_method: LOCAL_SMTP, dump_environment: false) ⇒ Mailer

Returns a new instance of Mailer.

Parameters:

  • to (String) (defaults to: "postmaster")

    The address to email error reports to.

  • from (String) (defaults to: DEFAULT_FROM)

    The from address for error reports.

  • subject (String) (defaults to: DEFAULT_SUBJECT)

    The subject template which can access attributes defined by ‘#attributes_for`.

  • delivery_method (Object) (defaults to: LOCAL_SMTP)

    The delivery method as required by the mail gem.

  • dump_environment (Boolean) (defaults to: false)

    Attach ‘env` as `environment.yaml` to the error report.



28
29
30
31
32
33
34
35
36
# File 'lib/utopia/exceptions/mailer.rb', line 28

def initialize(app, to: "postmaster", from: DEFAULT_FROM, subject: DEFAULT_SUBJECT, delivery_method: LOCAL_SMTP, dump_environment: false)
	@app = app
	
	@to = to
	@from = from
	@subject = subject
	@delivery_method = delivery_method
	@dump_environment = dump_environment
end

Instance Method Details

#call(env) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/utopia/exceptions/mailer.rb', line 50

def call(env)
	begin
		return @app.call(env)
	rescue => exception
		send_notification exception, env
		
		raise
	end
end

#freezeObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/utopia/exceptions/mailer.rb', line 38

def freeze
	return self if frozen?
	
	@to.freeze
	@from.freeze
	@subject.freeze
	@delivery_method.freeze
	@dump_environment.freeze
	
	super
end