Permalink
Browse files

Initial Rack middleware

  • Loading branch information...
1 parent a4dcaf5 commit 81437f0b482e130ad2dcbd77727d9f22350f3b63 @jferris jferris committed Jan 20, 2010
Showing with 75 additions and 0 deletions.
  1. +1 −0 lib/hoptoad_notifier.rb
  2. +22 −0 lib/hoptoad_notifier/rack.rb
  3. +52 −0 test/rack_test.rb
View
@@ -7,6 +7,7 @@
require 'hoptoad_notifier/notice'
require 'hoptoad_notifier/sender'
require 'hoptoad_notifier/backtrace'
+require 'hoptoad_notifier/rack'
# Gem for applications to automatically post errors to the Hoptoad of their choice.
module HoptoadNotifier
@@ -0,0 +1,22 @@
+module HoptoadNotifier
+ class Rack
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ begin
+ response = @app.call(env)
+ rescue Exception => raised
+ HoptoadNotifier.notify_or_ignore(raised)
+ raise
+ end
+
+ if env['rack.exception']
+ HoptoadNotifier.notify_or_ignore(env['rack.exception'])
+ end
+
+ response
+ end
+ end
+end
View
@@ -0,0 +1,52 @@
+require File.dirname(__FILE__) + '/helper'
+
+class RackTest < Test::Unit::TestCase
+
+ should "call the upstream app with the environment" do
+ environment = { 'key' => 'value' }
+ app = lambda { |env| ['response', {}, env] }
+ stack = HoptoadNotifier::Rack.new(app)
+
+ response = stack.call(environment)
+
+ assert_equal ['response', {}, environment], response
+ end
+
+ should "deliver an exception raised while calling an upstream app" do
+ HoptoadNotifier.stubs(:notify_or_ignore)
+
+ exception = build_exception
+ app = lambda do |env|
+ raise exception
+ end
+
+ begin
+ stack = HoptoadNotifier::Rack.new(app)
+ stack.call({})
+ rescue Exception => raised
+ assert_equal exception, raised
+ else
+ flunk "Didn't raise an exception"
+ end
+
+ assert_received(HoptoadNotifier, :notify_or_ignore) { |expect| expect.with(exception) }
+ end
+
+ should "deliver an exception in rack.exception" do
+ HoptoadNotifier.stubs(:notify_or_ignore)
+ exception = build_exception
+
+ response = [200, {}, ['okay']]
+ app = lambda do |env|
+ env['rack.exception'] = exception
+ response
+ end
+ stack = HoptoadNotifier::Rack.new(app)
+
+ actual_response = stack.call({})
+
+ assert_equal response, actual_response
+ assert_received(HoptoadNotifier, :notify_or_ignore) { |expect| expect.with(exception) }
+ end
+
+end

0 comments on commit 81437f0

Please sign in to comment.