Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you debug a Sinatra app like a Rails app?

In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form.

I have added:

puts params.inspect

and

set :logging, :true

The params.inspect works if everything goes well. But if an error happens before the controller is executed I'm not getting any information about the error like I would in Rails by default.

What's the best way to get useful debug info?

This example did not work at all (the app wouldn't even start after I added this code):

configure do 
  Log = Logger.new("sinatra.log")
  Log.level  = Logger::INFO 
end

followed by:

Log.info "#{@users.inspect}"
like image 968
nova Avatar asked Sep 01 '09 21:09

nova


3 Answers

You could try adding a before filter that prints out the parameters

before do   puts '[Params]'   p params end 
like image 79
BaroqueBobcat Avatar answered Sep 24 '22 04:09

BaroqueBobcat


My opinion is that for debug you should use configure :development do because some debugging flags are turned on in this scenario. So, under your configure do block you can enable the flags:

enable :logging, :dump_errors, :raise_errors 

and for your logging facility:

log = File.new("sinatra.log", "a") STDOUT.reopen(log) STDERR.reopen(log) 

From the Sinatra handbook:

  • dump_errors option controls whether the backtrace is dumped to rack.errors when an exception is raised from a route. The option is enabled by default for top-level apps.

  • raise_errors - allow exceptions to propagate outside of the app (...) The :raise_errors option is disabled by default for classic style apps and enabled by default for Sinatra::Base subclasses.

I also use the

puts "something" + myvar.inspect 

method in the middle of my controllers.

like image 39
include Avatar answered Sep 25 '22 04:09

include


As @jshen says, ruby-debug is a very nice tool - if you are using Ruby 1.8.

In order to use it in Sinatra, insert

require 'ruby-debug/debugger' 

where you'd like debugging to start.

like image 43
asymmetric Avatar answered Sep 22 '22 04:09

asymmetric