sinatra: Catchall error block fails to catch direct decendants of Exception

Because the conditional on this line checks for strictly less than instead of less than or equal to, error blocks are unable to catch any thing which is a direct descendant of Exception.

https://github.com/sinatra/sinatra/blob/eee711bce740d38a9a91aa6028688c9a6d74b23b/lib/sinatra/base.rb#L1153

Here’s a quick (untested) example:

require 'sinatra'

error do
  puts "Error block called!"
end

get "/" do
  raise StandardError
end

After visiting /, the error block will not be called. If this is by design, then this seems like a strange design decision. If it isn’t, then I’d be happy to open a PR for this.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for that thorough comment @carlwiedemann. I also like consistency.

I think 4. is what we can do short term. For a new major version we could consider the other options I think.

I’m interested to hear some thoughts from the rest of @sinatra/sinatras-helpers

Isn’t https://github.com/sinatra/sinatra/pull/1914 showing the opposite?

Sorry, let me explain what I meant. I was trying to say that when raise_errors is true, that the problem as originally described by @thesecretmaster would happen.

Is the bug that any error handlers are called when raise_errors are true? 🤔

That’s difficult to say. Here’s why: Consider the test added in https://github.com/sinatra/sinatra/commit/4e71959595796a7e2dff4b89e3dbbfc2f8e1abd7, which now appears on line 110 of mapped_error_test.rb

That test, which has been in the codebase for 13 years, seems to suggest that error handlers should be invoked when raise_errors is true, and that the error handler text should appear in the HTTP response body.

Based on that information, I added an additional test, which demonstrates that the error handler is not invoked if the handler is declared for Exception, or is a catch-all.

So, I would expect the test that I added in https://github.com/sinatra/sinatra/pull/1914 to also pass.

If you compare the original test that I mentioned above from 4e71959595796a7e2dff4b89e3dbbfc2f8e1abd7, with the test that I added, you can see the only material difference is the class that the error handler is invoked for – the original test uses FooError, while my test uses Exception.

Here they are together (as they appear in the test file):

    it "calls error handlers before raising errors even when raise_errors is set" do
      mock_app do
        set :raise_errors, true
        error(FooError) { "she's there." }
        get('/') { raise FooError }
      end
      get '/'
      assert_equal 500, status
      assert_equal "she's there.", body
    end

    it "uses the Exception handler before raising errors even when raise_errors is set" do
      mock_app do
        set :raise_errors, true
        error(Exception) { "Parent handler" }
        get('/') { raise FooError }
      end
      get '/'
      assert_equal 500, status
      assert_equal "Parent handler", body
    end

It seems to me that both of these tests should pass. Right now only the first test passes, while the second test fails. What do you think?

If you agree, I am happy to work on https://github.com/sinatra/sinatra/pull/1914 to achieve that result.

@thesecretmaster Thanks for filing an issue.

What’s your motivation to use StandardError? Even the default raise (IOW raise with no argument) raises RuntimeError (it’s a subclass of StandardError) instead of raw StandardError.

If you have a good justification regarding your motivation and have a good patch (this meant the patch must not break other tested behavior), please submit a PR. Let’s discuss based on the patch.

P.S.

  1. I wouldn’t like to break the compatibility, so even if your proposal goes well, I may not merge that until v3.0.0.
  2. Your example needs to set :show_exceptions, false