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.



45
46
47
48
49
50
51
52
53
# File 'lib/utopia/exceptions/mailer.rb', line 45

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



67
68
69
70
71
72
73
74
75
# File 'lib/utopia/exceptions/mailer.rb', line 67

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

#freezeObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/utopia/exceptions/mailer.rb', line 55

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