The problem is that the default handler is defined in Sinatra::ShowExceptions, which is controlled by the :raise_errors configuration setting, and this setting is all or nothing. If :raise_errors is off then the error method can be used in the Sinatra app, but it has to be used for all exceptions, and there is no access to the error handler defined in Sinatra::ShowExceptions. If it is on, then all exceptions get handled by the ShowExceptions middleware.
The rationale for what I want to do is that, at least during development, I want "expected" errors to be handled in a user-friendly fashion and I want "unexpected" errors to be handled in a developer-friendly fashion.
I'm guessing that the only way to do this is to define a rack middleware class that inherits from Sinatra::ShowExceptions, and which has some extra option in its use method to specify which classes of exceptions to handle or not handle.
Is there any easier way that I am missing?
(I'm using jruby, if that makes any difference.)
I found that this seems to work:
set :raise_errors, false
set :show_exceptions, false
...
error BaseErrorClassForMySpecialErrors do
# ... special handling for sub-classes of this error class
end
$showExceptions = Sinatra::ShowExceptions.new(self)
error do
@error = env['sinatra.error']
$showExceptions.pretty(env, @error)
end
In other words, set :raise_errors and :show_exceptions to false to turn off the ShowExceptions handling of errors, then create a separate instance of Sinatra::ShowExceptions, and call its pretty method in the error do catch-all error handler.
Hopefully creating an extra instance of Sinatra::ShowExceptions for this purpose doesn't have any unintended side-effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With