Is there a way in Sinatra to use a custom error handler for some errors and a default handler for all other errors?

The problem is that the default handler is defined in Sinatra :: ShowExceptions , which is controlled by the configuration parameter : raise_errors , and this parameter is all or nothing. If the : raise_errors parameter is disabled, the error method can be used in the Sinatra application, but it should be used for all exceptions, and there is no access to the error handler defined in Sinatra :: ShowExceptions . If it is enabled, all exceptions are handled by ShowExceptions middleware.

The rationale for what I want to do is that, at least during development, I want the "expected" errors to be handled in a friendly manner, and I want the "unexpected" errors to be handled in a friendly way to the developers.

I assume that the only way to do this is to define a rack middleware class that inherits from Sinatra :: ShowExceptions , and which has an additional option in its use to indicate which exception classes to handle or not to process.

Is there an easier way that I am missing?

(I use jruby if that matters.)

+5
source share
2 answers

I found this 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

, : raise_errors : show_exceptions false, ShowExceptions, Sinatra:: ShowExceptions pretty do.

, Sinatra:: ShowExceptions .

+6

set: show_exceptions,: after_handler . : dump_errors, false , .

0

All Articles