We create positive world change connecting authentic companies with real people in socially responsible ways.

image placeholder

How to Read a Stack Trace

October 05, 2009 by Brandon Weiss

I come from a strictly web development background. I’m not a computer science major, nor have I ever really used any software development languages besides in the requisite C++/Java class that every developer seems to take in high school or college. Since I started on the web rather than in software, my first programming language was PHP, so I hadn’t even heard the term ‘stack trace’ until I started using Ruby on Rails.

For those not in the know, a stack trace is really just an error report. You run your application, something goes wrong and it spits out a stack trace so you can figure out what it is. The reason it’s called a ‘trace’ is because rather than just showing the line and file the error occurred in, it actually traces the path through the program that was taken to get to the error, which can be useful in debugging.

Reading a stack trace isn’t hard; it’s one of those things that once you know how seems extremely obvious, but at first is a little unclear, especially if you come from a PHP-based web development background like myself. Here’s an example:

dev:fb brandon$ script/server
=> Booting WEBrick
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- acts_as_ferret (MissingSourceFile)
  from /usr/.../custom_require.rb:31:in `require'
  from /usr/.../dependencies.rb:156:in `require'
  from /usr/.../dependencies.rb:521:in `new_constants_in'
  from /usr/.../dependencies.rb:156:in `require'
  from /Users/brandon/Sites/fb/config/environment.rb:64
  from /usr/.../custom_require.rb:31:in `gem_original_require'
  from /usr/.../custom_require.rb:31:in `require'
  from /usr/.../dependencies.rb:156:in `require'
  from /usr/.../dependencies.rb:521:in `new_constants_in'
  from /usr/.../dependencies.rb:156:in `require'
  from /usr/.../commands/server.rb:84
  from /usr/.../custom_require.rb:31:in `gem_original_require'
  from /usr/.../custom_require.rb:31:in `require'
  from script/server:3

Woah, what the hell is all that? It looks intimidating, but it’s really not. You can ignore the ellipses in the paths; they aren’t normally there, I just cut the paths short for brevity’s sake.

So let’s start at the top. script/server is a script that starts up a very simple Ruby server called WEBrick (used for development only). You can see WEBrick starting to boot up and then a whole bunch of FAIL. The stack trace is ordered in reverse chronology, so it starts at the bottom and ends at the top. If you look at the bottom you can see the script I initially called. Then lots of stuff happens. These aren’t errors, it’s just the path that’s being taken through the program. After the line about the Rails application starting you’ll see a summary:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- acts_as_ferret (MissingSourceFile)

This was the most confusing part for me, because in PHP, an error generally looks something like this:

Warning: Invalid argument supplied for foreach() in /www/yourserver/html/index.php on line 42

This is fairly unambiguous. On line 42 in index.php the argument given to the foreach() is bad. But the stack trace summary isn’t as simple as that. If you look at the path, that’s not even a file in my application; it’s a file that’s part of RubyGems, the plugin system for Ruby. Obviously there’s not an error in RubyGems (well I suppose there could be, but let’s assume there isn’t). At the end of the summary, you’ll see the error itself:

no such file to load -- acts_as_ferret

A quick Google search shows that acts_as_ferret is a Ruby gem (plugin) for implementing full text search using Ferret. OK, so clearly the acts_as_ferret gem is missing. You could just sudo install gem acts_as_ferret and the next time you run script/server the error would most likely be gone. But something is amiss. Rails has this awesome dependency architecture where you can specify gems that your app requires, and if they aren’t there, Rails will nicely let you know that; it shouldn’t be throwing an error like this.

To find the location of the problem, you actually need to look for the last line (chronologically) in the stack trace that has a path referencing a file in your app. In my case there’s only one line, and it is:

from /Users/brandon/Sites/fb/config/environment.rb:64

If I go to line 64 in environment.rb I see this:

require 'acts_as_ferret'

And there’s the problem. The gem was being referenced incorrectly. In your environment.rb file the syntax for indicating a dependency would be something like:

config.gem 'acts_as_ferret'

Awesome, now when I run script/server I get:

dev:fb brandon$ script/server
=> Booting WEBrick
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
Missing these required gems:
  acts_as_ferret  
  campaign_monitor  
  flickraw  

You're running:
  ruby 1.8.7.174 at /usr/local/bin/ruby
  rubygems 1.3.5 at /Users/brandon/.gem/ruby/1.8, /usr/local/lib/ruby/gems/1.8

Run `rake gems:install` to install the missing gems.

Comments

image placeholder