Reputation: 107
How do I specify a global rescue in case of an exception, and if you use Sinatra for an API or application, how do you handle logging?
Upvotes: 5
Views: 4344
Reputation: 35914
I had trouble getting this working out of the box in my dev environment - to get it to work, I had to set show_exceptions
to false in my sinatra config.
class BaseApp < Sinatra::Base
configure { set :show_exceptions, false }
error do |err|
raise "Error: #{err}"
end
end
This setting, when set to true, enables error pages that show backtrace and environment information when an unhanded exception occurs, but I could only fire custom errors by disabling it.
Upvotes: 5
Reputation: 11198
404s can be handled with the help of the not_found
method like eg:
not_found do
'Site does not exist.'
end
500s can be handled by calling the error method with a block, eg:
error do
"Application error. Pls try later."
end
The details of the error can be accessed via the sinatra.error
in request.env
like so:
error do
'An error occured: ' + request.env['sinatra.error'].message
end
Upvotes: 7