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
ATTACHMENT_SIZE_LIMIT =
64*1024
SENSITIVE_FIELD =
/authorization|cookie|credential|password|private[_-]?key|referer|referrer|secret|session|token|variables|api[_-]?key/i
REDACTED =
"[REDACTED]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, to: "postmaster", from: DEFAULT_FROM, subject: DEFAULT_SUBJECT, delivery_method: LOCAL_SMTP, dump_body: false, dump_environment: false, attachment_size_limit: ATTACHMENT_SIZE_LIMIT, redact: SENSITIVE_FIELD) ⇒ 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_body (Boolean) (defaults to: false)

    Attach a bounded rewindable request body to the error report.

  • dump_environment (Boolean) (defaults to: false)

    Include application state and attach it as state.yaml.

  • attachment_size_limit (Integer) (defaults to: ATTACHMENT_SIZE_LIMIT)

    The maximum size of each attachment.

  • redact (Regexp | Nil) (defaults to: SENSITIVE_FIELD)

    A pattern matching structured field names whose values should be redacted.



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

def initialize(app, to: "postmaster", from: DEFAULT_FROM, subject: DEFAULT_SUBJECT, delivery_method: LOCAL_SMTP, dump_body: false, dump_environment: false, attachment_size_limit: ATTACHMENT_SIZE_LIMIT, redact: SENSITIVE_FIELD)
	super(app)
	
	@to = to
	@from = from
	@subject = subject
	@delivery_method = delivery_method
	@dump_body = dump_body
	@dump_environment = dump_environment
	@attachment_size_limit = Integer(attachment_size_limit)
	@redact = redact
	
	if @attachment_size_limit < 0
		raise ArgumentError, "attachment_size_limit must not be negative!"
	end
end

Instance Method Details

#call(request) ⇒ Object

Report application exceptions by email before returning an error response.



78
79
80
81
82
83
84
85
86
87
# File 'lib/utopia/exceptions/mailer.rb', line 78

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

#freezeObject

Freeze this object and its internal state.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/utopia/exceptions/mailer.rb', line 60

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