Sinatra Catching Exceptions for Custom Error Pages - ruby

I am trying to handle errors in a Modular Sinatra App. We raise various error through out the app and I wrote some thing alike to catch errors thinking it will happen hierarchically.
My the file I use to error handle looks like the following.
#!/usr/bin/env ruby
# #class class RApp
# #brief Sinatra main routes overloading for App class
class RApp < Sinatra::Base
# #fn not_found do {{{
# #brief Not found error page
not_found do
render_error 404, _('Not Found'), env['sinatra.error'].message
end # }}}
# #fn error ServiceNotAvailableError do {{{
# #brief Handle ServiceNotFoundException, commonly associated with communication
# errors with external required services like Ambrosia, Saba
error ServiceNotAvailableError do
render_error 503, _('Service Not Available'), env['sinatra.error'].message
end # }}}
# #fn error Exception do {{{
# #brief Handle general internal errors
error Exception do
render_error 500, _('Internal Server Error'), env['sinatra.error'].message
end # }}}
error DBC::InvalidUUIDError do
"Invalid UUID Error"
end
# #fn def show_error code, title, message, view = nil {{{
# #brief Displays the proper message (html or text) based on if the request is XHR or otherwise
def render_error code, title, message, view = nil
view = code.to_s if view.nil?
if request.xhr?
halt code, message
else
halt code, slim(:"error_pages/#{view}", locals: {title: title, message: message})
end
end # }}}
# Just for testing
get '/errors/:type' do |type|
raise Object.const_get(type)
end
end # of class RApp < Sinatra::Base }}}
# vim:ts=2:tw=100:wm=100
I was thinking it will try in the order in the file which seem to be true.
How ever the issue is Exception doesn't catch all exceptions.
For example I have DBC::InvalidUUIDError which is as such
DBC::InvalidUUIDErrror < DBC::Error
DBC::Error < RuntimeError
which I understand as in Ruby RuntimeError < Exception
But it error Exception doesn't catch all Exception as I thought.
Am I doing some thing wrong? Or is it not possible to catch all exceptions generally?
Note: Additionally to the answers provided (both works), I had set :raise_errors, true. You need to not set it in development and production. By default it is set for 'test' env.
Nature of the matter was some exceptions been handled and some not.

Add this to prevent Sinatra's own error page from interfering with your custom error handlers:
set :show_exceptions, false
By default, the setting is true in development mode, and false in production.
Note that if you follow the Sinatra readme's advice about setting set :show_exceptions, :after_handler, that will enable your error handlers to run even in development mode (at least for some exception classes), but it will also enable the built-in error page in production for uncaught exceptions. And it's unclear to me which error handlers it will respect, and which ones it will ignore in favor of the built-in debug page.
Edit: I realised you also asked about the order the error handlers are defined in. It doesn't matter; Sinatra looks for exact matches to the exception class in the app class first, and then in its superclasses. If it doesn't find any, it repeats the search for the exception's superclass, etc. So the handler for Exception will only get called for exceptions for which there isn't a closer match.

Here is the relevant info from the Sinatra docs:
Sinatra installs special not_found and error handlers when running under the development environment to display nice stack traces and additional debugging information in your browser.
What this means, is that when you are running in development, Sinatra has a creates a default "catch-all" error handler which has a higher precedence than your error Exception do .. end block.
Once you enter production mode, (or disable in dev via disable :show_exceptions
), your error Exception do .. end block should catch all of your exceptions.
Note that the order of these error blocks defined is irrelevant.

Related

Mongoid::Errors::DocumentNotFound is raised even after rescuing

I have a Rails controller with this method being triggered as a before_action:
def authenticate_user
Knock::AuthToken.new(token: token).entity_for(User)
rescue Mongoid::Errors::DocumentNotFound
render nothing: true, status: 401
end
Even though I can verify that it's rescuing the error(a byebug breakpoint gets triggered under the rescue statement), it still manages to be raised immediately after:
Mongoid::Errors::DocumentNotFound (
message:
Document(s) not found for class User with id(s) 1.
summary:
When calling User.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 1 ... (1 total) and the following ids were not found: 1.
resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.):
app/controllers/api/base_controller.rb:12:in `authenticate_user'
I've been using the rescue keyword for years in Ruby and have never encountered this.
What I'm running:
Ruby 2.5
Rails 5.2
Mongoid 7.0.1
Why is it that the error gets raised even when I rescue it, and how can I prevent the error from getting raised?
I'm not sure why it gets raised even when you rescue it, but I'll answer the "how to prevent it" part.
In your mongoid.yml, you need to set
raise_not_found_error: false
See this section in the Mongoid docs for an example.

Rescuing error does nothing?

I'm trying to rescue an exception with this method:
def template_deleted
mailchimp_client.templates.info(mailchimp_id)
rescue Mailchimp::InvalidTemplateError => error
puts "Template deleted in Mailchimp: #{error}"
return true
else
return false
end
And no matter what I use to output the message, whether it's STDERR, STDOUT, log.error, p, puts, or print, nothing gets out to the environment's log. This should definitely be returning an error, because the template definitely doesn't exist in Mailchimp.
When I try the same code in the console I can read the error just fine, so either there's something wrong with the rescuing itself (i.e., my method is returning false which it shouldn't), or there's something wrong with the way I'm outputting it.
To output something in the log file of the current environment, use the Rails logger like this:
logger.debug "Template deleted in Mailchimp: #{error}"
You can replace the debug method call with any logging level name, that are briefly described in the link above. Also don't forget to make sure you're running in correct environment!

Sinatra - Why does 404 error block never trigger?

Here is a simple app:
class App < Sinatra::Base
set :show_exceptions, false
not_found do
slim :err_404
end
post "/doit" do
user ||= User.find(params["userid"]) || halt(404)
end
end
When given an invalid userid, the 404 error block should trigger and then render the 404 page. Instead, Sinatra shows "Internal Server Error" on the page, and this stacktrace is printed to console:
Problem:
Document(s) not found for class User with id(s) 53d06a8ca7b7d52d11300003.
Summary:
....
I'm guessing the halt(404) isn't being called. I was following this blog post about error handling, so why wouldn't the same thing work here?
Assuming you are using ActiveRecord, User.find(params["userid"]) will raise an ActiveRecord::RecordNotFound exception if there is no matching record. This exception is thrown before the 404 handler gets to run and ends the route handling, resulting in the internal server error.
To fix it you could check for the exception, and call the 404 handler if it is raised:
post "/doit" do
begin
user ||= User.find(params["userid"]
rescue ActiveRecord::RecordNotFound
halt(404)
end
end
A better solution might be to use find_by_id instead, which avoids using exceptions for flow control:
post "/doit" do
user ||= User.find_by_id(params["userid"]) || halt(404)
end

get the backtrace in a sinatra app

Im trying to get the backtrace in sinatra in case of an error.
I know rails has one in
Rails.respond_to?(:backtrace_cleaner)
and I saw that sinatra is suppose to have one (by default enabled) in STDERR
So i tried
STDERR.inspect
and I got #<IO:<STDERR>>
When rescuing the exception, catch the exception object.
begin
raise "hello"
rescue => e
e.backtrace
end
In Ruby you can call a method caller at any place and get a full backtrace as an array.

Why do I not see the thrown exceptions in a Cucumber "Around"?

I have a set of cucumber tests that get run on a build server.
I often want faster feedback than the server directly provides and so I watch the console output as it runs. I was wanting a way of identifying any failing test with a single search term so I modified our Around to print "Failed Test" on any exception, but Ruby doesn't seem to be handing the exception back up to the around. I've verified this by having puts statements after the begin ... end.
Does anyone know why this is happening or a way of wrapping any exception thrown from a failing test in a begin?
Around() do |scenario, block|
begin
Timeout.timeout(0.1) do
block.call
end
rescue Timeout::Error => e
puts "Failed Test"
puts caller
rescue Exception => e
puts "Failed Test"
raise e
end
end
Looking at cucumber 1.3.12 it actually rescues any exceptions from scenario steps. So you can't see them in any way without modifying cucumber gem.
See my answer on how to put a debug hook in that place for more information:
https://stackoverflow.com/a/22654786/520567
Have you tried disabling Cucumber's exception capturing with the #allow-rescue tag?
#allow-rescue: Turns off Cucumber’s exception capturing for the tagged scenario(s). Used when the code being tested is expected to raise and handle exceptions.
https://github.com/cucumber/cucumber/wiki/Tags

Resources