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.
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
- Describe `raise_errors` better in README (#1917) Related to https://github.com/sinatra/sinatra/issues/1548 — committed to sinatra/sinatra by carlwiedemann 4 years ago
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
Sorry, let me explain what I meant. I was trying to say that when
raise_errorsis true, that the problem as originally described by @thesecretmaster would happen.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_errorsis 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 usesException.Here they are together (as they appear in the test file):
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 defaultraise(IOW raise with no argument) raisesRuntimeError(it’s a subclass ofStandardError) instead of rawStandardError.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.
set :show_exceptions, false