What is a stack trace?
Stack trace or backtrace is a representation of the call stack at any point of
time while running the program.
Stack trace will be printed when an exception is raised. So let's raise an error
with a few method calls to understand it.
To see a very simple stack trace open rails console
and paste the following
code:
You should have got the following output on your console:
It is very evident from the stack trace that the method capitalize_name
is at
the top of the call stack and encountered an error
NoMethodError (undefined method upcase' for 1:Integer)
at (irb):2
, and
print_name
is at the position 2
of the stack.
How to log stack trace of an exception
If we want to log the stack trace of the exception rescued in our application,
we need to set up a logger first.
Rails makes use of the ActiveSupport::Logger
class to write log information.
Other loggers, such as Log4r
, may also be substituted.
You can specify an alternative logger in config/application.rb
or any other
environment file, for example:
Or you can use the following:
The following will iterate through each line of backtrace and logs to log file:
Interpreting Rails stack trace
Let's change the task controller's show
method as follows:
Now try to edit an existing task, check the console of your browser, and we can
see a 500 error message. We can see an error page in the preview of particular a
request in Network
tab. The lines after
Application Trace | Framework Trace | Full Trace
is our stack trace:
The above error message says Unrecognized status code :OK
, but 200 OK
is a
standard status code part of http as we know. So let us check why Rails has
raised such an error. But the problem is that there is no stack trace printed
for us to debug.
Rails isn't showing the framework stack trace by default, since it has a
silencer enabled to show only the app directory stack trace and to skip the
Rails stack trace. So let's remove the silencers.
open config/initializers/backtrace_silencers.rb
and uncomment the following
line:
Initializer files are loaded only once when the Rails server is started. Always
restart the server, when any of the initializer files are modified. For further
information check the
documentation.
So let's restart the Rails server and try editing the task once again:
We can look at the first line of stack trace, and it shows:
rack (2.2.3) lib/rack/utils.rb:583:in
block in status_code'`
So this is an error from rack version 2.2.3, go to GitHub rack
repo and select
the tag 2.2.3
and navigate to lib/rack/utils.rb
.
Line 583 is calling a method SYMBOL_TO_STATUS_CODE
. If we look into the code
we can see they have already defined all the status codes in
HTTP_STATUS_CODES
, and there is a 200 => 'OK'
in the list. But in the
SYMBOL_TO_STATUS_CODE
method it is getting down cased(Refer
here).
So we have to use ok
symbol instead of OK
:
We are not committing any of these changes:
Video on how to interpret a stack trace
Chris Oliver of gorails has posted a nice video on
how to interpret a stack trace.