Skip to content

Commit c3fa8a7

Browse files
committed
Use values set for the middleware as defaults
ExceptionNotifier works OK as long as you don't have to manually call the `Notifier#exception_notification` method yourself and rely on the middleware to do your job. However, if you are using a Rails action to show 500 errors (e.g. you are using the `rescue_from` helper in your ApplicationController), then you will have to reconfigure the ExceptionNotifier options. This means that in your controller you'd have something like: rescue_from Exception, :with => :server_error def server_error(exception) request.env['exception_notifier.options'] = { :sender_address => "[email protected]", :exception_recipients => "[email protected]" } ExceptionNotifier::Notifier.exception_notification( request.env, exception ).deliver end ... which means code duplication, especially if you have more than one action that's handling exceptions. This commit should solve this problem by making the default attributes writable and use the initial middleware configuration to set those defaults to something more sensible.
1 parent 37b5bd2 commit c3fa8a7

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/exception_notifier.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ def self.default_ignore_exceptions
1212

1313
def initialize(app, options = {})
1414
@app, @options = app, options
15+
16+
Notifier.default_sender_address = @options[:sender_address]
17+
Notifier.default_exception_recipients = @options[:exception_recipients]
18+
Notifier.default_email_prefix = @options[:email_prefix]
19+
Notifier.default_sections = @options[:sections]
20+
1521
@options[:ignore_exceptions] ||= self.class.default_ignore_exceptions
1622
end
1723

1824
def call(env)
1925
@app.call(env)
2026
rescue Exception => exception
21-
options = (env['exception_notifier.options'] ||= {})
27+
options = (env['exception_notifier.options'] ||= Notifier.default_options)
2228
options.reverse_merge!(@options)
2329

2430
unless Array.wrap(options[:ignore_exceptions]).include?(exception.class)

lib/exception_notifier/notifier.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ class Notifier < ActionMailer::Base
77
self.append_view_path "#{File.dirname(__FILE__)}/views"
88

99
class << self
10+
attr_writer :default_sender_address
11+
attr_writer :default_exception_recipients
12+
attr_writer :default_email_prefix
13+
attr_writer :default_sections
14+
1015
def default_sender_address
11-
%("Exception Notifier" <[email protected]>)
16+
@default_sender_address || %("Exception Notifier" <[email protected]>)
1217
end
1318

1419
def default_exception_recipients
15-
[]
20+
@default_exception_recipients || []
1621
end
1722

1823
def default_email_prefix
19-
"[ERROR] "
24+
@default_email_prefix || "[ERROR] "
2025
end
2126

2227
def default_sections
23-
%w(request session environment backtrace)
28+
@default_sections || %w(request session environment backtrace)
2429
end
2530

2631
def default_options

0 commit comments

Comments
 (0)