Class: Utopia::Exceptions::Mailer

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

Overview

A middleware which catches application exceptions and sends an email containing the exception, backtrace, request, and application state.

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 request attributes as attributes.yaml to the error report.



32
33
34
35
36
37
38
39
40
# File 'lib/utopia/exceptions/mailer.rb', line 32

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

Instance Method Details

#call(request) ⇒ Object

Report application exceptions by email before returning an error response.



59
60
61
62
63
64
65
66
67
68
# File 'lib/utopia/exceptions/mailer.rb', line 59

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

#freezeObject

Freeze this object and its internal state.



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

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