views:

2120

answers:

6

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}"
+2  A: 

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

before do
  puts '[Params]'
  p params
end
BaroqueBobcat
+1  A: 

Have you thought about trying something like this article: http://www.gittr.com/index.php/archive/logging-with-sinatra-and-passenger-another-try/

Pie
+1  A: 

I'm guessing since you mentioned your main Sinatra controller, you have more than one, which means you're subclassing Sinatra::Base rather than using a classic (top-level) Sinatra app. If this is indeed the case, a lot of the default error-handling that Sinatra does is disabled by default.

If you include set :show_exceptions, true if development? in the class definition, you'll get friendly error pages that include a stack trace, params, etc, instead of just an internal server error. Another option is to set :raise_errors, false and then include a error do ... end block that will run whenever one of your routes raises an error.

Emily
+2  A: 

I'd highly recommend using ruby-debug. It's very easy to setup and use once you learn the 4 or 5 basic commands. It will allow you to set a breakpoint where you want to inspect the params and interactively play with it like you would in IRB.

jshen
+5  A: 

Hi nova,

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 this 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 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 that

puts "something" + myvar.inspect

method in the middle of my controllers :)

have fun Francisco

include
+3  A: 

As @jshen says, ruby-debug is a very nice tool.

In order to use it in sinatra, insert require 'ruby-debug/debugger' where you'd like debugging to start.

asymmetric