Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

error handlers always override raise_errors option

  • Loading branch information...
commit 4e71959595796a7e2dff4b89e3dbbfc2f8e1abd7 1 parent 3ff2262
@rtomayko rtomayko authored
Showing with 29 additions and 6 deletions.
  1. +7 −0 CHANGES
  2. +9 −4 lib/sinatra/base.rb
  3. +13 −2 test/mapped_error_test.rb
View
7 CHANGES
@@ -47,6 +47,13 @@
of issues with extensions that need to add any of these things
to the base class. (Ryan Tomayko)
+ * Exception error handlers always override the raise_errors option now.
+ Previously, all exceptions would be raised outside of the application
+ when the raise_errors option was enabled, even if an error handler was
+ defined for that exception. The raise_errors option now controls
+ whether unhandled exceptions are raised (enabled) or if a generic 500
+ error is returned (disabled). (Ryan Tomayko)
+
* The X-Cascade response header is set to 'pass' when no matching route
is found or all routes pass. (Josh Peek)
View
13 lib/sinatra/base.rb
@@ -606,10 +606,16 @@ def handle_exception!(boom)
@env['sinatra.error'] = boom
dump_errors!(boom) if settings.dump_errors?
- raise boom if settings.raise_errors? || settings.show_exceptions?
+ raise boom if settings.show_exceptions?
@response.status = 500
- error_block! boom.class, Exception
+ if res = error_block!(boom.class)
+ res
+ elsif settings.raise_errors?
+ raise boom
+ else
+ error_block!(Exception)
+ end
end
# Find an custom error block for the key(s) specified.
@@ -619,8 +625,7 @@ def error_block!(*keys)
while base.respond_to?(:errors)
if block = base.errors[key]
# found a handler, eval and return result
- res = instance_eval(&block)
- return res
+ return instance_eval(&block)
else
base = base.superclass
end
View
15 test/mapped_error_test.rb
@@ -55,10 +55,9 @@ def test_default
assert_equal 'looks good', body
end
- it "raises without calling the handler when the raise_errors options is set" do
+ it "raises errors from the app when raise_errors set and no handler defined" do
mock_app {
set :raise_errors, true
- error(FooError) { "she's not there." }
get '/' do
raise FooError
end
@@ -66,6 +65,18 @@ def test_default
assert_raise(FooError) { get '/' }
end
+ it "calls error handlers before raising errors even when raise_errors is set" do
+ mock_app {
+ set :raise_errors, true
+ error(FooError) { "she's there." }
+ get '/' do
+ raise FooError
+ end
+ }
+ assert_nothing_raised { get '/' }
+ assert_equal 500, status
+ end
+
it "never raises Sinatra::NotFound beyond the application" do
mock_app {
set :raise_errors, true

1 comment on commit 4e71959

@nateware

Excellent decision. Best of both worlds.

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