Skip to content
This repository
Browse code

error handlers always override raise_errors option

  • Loading branch information...
commit 4e71959595796a7e2dff4b89e3dbbfc2f8e1abd7 1 parent 3ff2262
Ryan Tomayko rtomayko authored
7 CHANGES
@@ -47,6 +47,13 @@
47 47 of issues with extensions that need to add any of these things
48 48 to the base class. (Ryan Tomayko)
49 49
  50 + * Exception error handlers always override the raise_errors option now.
  51 + Previously, all exceptions would be raised outside of the application
  52 + when the raise_errors option was enabled, even if an error handler was
  53 + defined for that exception. The raise_errors option now controls
  54 + whether unhandled exceptions are raised (enabled) or if a generic 500
  55 + error is returned (disabled). (Ryan Tomayko)
  56 +
50 57 * The X-Cascade response header is set to 'pass' when no matching route
51 58 is found or all routes pass. (Josh Peek)
52 59
13 lib/sinatra/base.rb
@@ -606,10 +606,16 @@ def handle_exception!(boom)
606 606 @env['sinatra.error'] = boom
607 607
608 608 dump_errors!(boom) if settings.dump_errors?
609   - raise boom if settings.raise_errors? || settings.show_exceptions?
  609 + raise boom if settings.show_exceptions?
610 610
611 611 @response.status = 500
612   - error_block! boom.class, Exception
  612 + if res = error_block!(boom.class)
  613 + res
  614 + elsif settings.raise_errors?
  615 + raise boom
  616 + else
  617 + error_block!(Exception)
  618 + end
613 619 end
614 620
615 621 # Find an custom error block for the key(s) specified.
@@ -619,8 +625,7 @@ def error_block!(*keys)
619 625 while base.respond_to?(:errors)
620 626 if block = base.errors[key]
621 627 # found a handler, eval and return result
622   - res = instance_eval(&block)
623   - return res
  628 + return instance_eval(&block)
624 629 else
625 630 base = base.superclass
626 631 end
15 test/mapped_error_test.rb
@@ -55,10 +55,9 @@ def test_default
55 55 assert_equal 'looks good', body
56 56 end
57 57
58   - it "raises without calling the handler when the raise_errors options is set" do
  58 + it "raises errors from the app when raise_errors set and no handler defined" do
59 59 mock_app {
60 60 set :raise_errors, true
61   - error(FooError) { "she's not there." }
62 61 get '/' do
63 62 raise FooError
64 63 end
@@ -66,6 +65,18 @@ def test_default
66 65 assert_raise(FooError) { get '/' }
67 66 end
68 67
  68 + it "calls error handlers before raising errors even when raise_errors is set" do
  69 + mock_app {
  70 + set :raise_errors, true
  71 + error(FooError) { "she's there." }
  72 + get '/' do
  73 + raise FooError
  74 + end
  75 + }
  76 + assert_nothing_raised { get '/' }
  77 + assert_equal 500, status
  78 + end
  79 +
69 80 it "never raises Sinatra::NotFound beyond the application" do
70 81 mock_app {
71 82 set :raise_errors, true

1 comment on commit 4e71959

Nate Wiger

Excellent decision. Best of both worlds.

Please sign in to comment.
Something went wrong with that request. Please try again.