Skip to content

Commit

Permalink
Add additional context to background exception notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
hwrd committed Oct 25, 2011
1 parent 2e36818 commit b9c4810
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
exception_notification (2.4.1)
exception_notification (2.5.2)
actionmailer (>= 3.0.4)

GEM
Expand Down
5 changes: 3 additions & 2 deletions lib/exception_notifier/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ def exception_notification(env, exception)
end
end

def background_exception_notification(exception)
def background_exception_notification(exception, additional = {})
if @notifier = Rails.application.config.middleware.detect{ |x| x.klass == ExceptionNotifier }
@options = (@notifier.args.first || {}).reverse_merge(self.class.default_options)
@exception = exception
@backtrace = exception.backtrace || []
@sections = %w{backtrace}
@additional_context = additional
@sections = %w(additional_context backtrace)
subject = compose_subject(exception)

mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% max = @additional_context.keys.max { |a, b| a.length <=> b.length } -%>
<% @additional_context.keys.sort.each do |key| -%>
* <%= raw("%-*s: %s" % [max.length, key, inspect_object(@additional_context[key])]) %>
<% end -%>
9 changes: 7 additions & 2 deletions test/background_exception_notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class BackgroundExceptionNotificationTest < ActiveSupport::TestCase
setup do
begin
@troublesome_object = "I like to cause problems."
1/0
rescue => e
@exception = e
@mail = ExceptionNotifier::Notifier.background_exception_notification(@exception)
@mail = ExceptionNotifier::Notifier.background_exception_notification(@exception, :the_trouble_object => @troublesome_object)
end
end

Expand Down Expand Up @@ -34,8 +35,12 @@ class BackgroundExceptionNotificationTest < ActiveSupport::TestCase
assert @mail.body.include? "A ZeroDivisionError occurred in background"
end

test "mail should contain additional context" do
assert @mail.body.include? "I like to cause problems."
end

test "mail should contain backtrace in body" do
assert @mail.body.include? "test/background_exception_notification_test.rb:6"
assert @mail.body.include? "test/background_exception_notification_test.rb:"
end

test "mail should not contain any attachments" do
Expand Down

0 comments on commit b9c4810

Please sign in to comment.