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

Viewed 35954

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}"
10 Answers

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.

I'd highly recommend using ruby-debug for Ruby 1.8. It's very easy to setup and use once you learn the four or five 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.

Ruby Debuggers in Sinatra

You are free to use Ruby debuggers for Sinatra. The main issue is that you will need to know which Ruby Gem is used for which Ruby version. For most people now that means Ruby 2.X and byebug.

Ruby Debuggers by Ruby version

 Ruby Version   Gem Install      require                add breakpoint
 ---------------------------------------------------------------------
 1.8.X          ruby-debug      'ruby-debug/debugger'   debugger
 1.9.X          debugger        'debugger'              debugger
 2.0.0+         byebug          'byebug'                byebug

Once you know that you must install the gem, require it in your code and then add breakpoints by typing a keyword. Taking byebug as an example:

  1. install: gem install byebug
  2. require: require 'byebug'
  3. breakpoint: byebug

Further Reading

Byebug project

Byebug guide to use a terminal debugger

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.

I'd recommend using the debugger gem for Ruby 1.9.

To use this with your Sinatra app:

  1. Add the gem to your Gemfile

    gem "debugger"
    
  2. Install the gem by running

    bundle
    
  3. In your main sinatra app file, add

    require 'debugger'
    
  4. Now to debug, you just have to add the following line

    debugger
    

    anywhere in your code.

When the code is being executed and debugger is seen, code would stop and you can inspect variables, run commands (using eval), etc.

Related