Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable stack trace display in Rails console

Is there any way to reduce the verbosity of error reporting in the rails console? Specifically, turn off the stack trace display? It's of no use most of the time, and downright annoying when I'm suffering from a case of the stupid fingers.

When I type something like:

MyModel.vtrsyr

I don't need a stack trace to tell me that there is no 'vtrsyr' method

like image 496
Ian Avatar asked Dec 04 '15 22:12

Ian


People also ask

How do I disable stack trace?

To stop getting stack trace for messages that are issued by the server or storage agent, the stack trace for these messages must be disabled. Issue the MSGSTACKTRACE DISABLE < messageNumber > command to disable one or more messages. The < messageNumber > might be a space-delimited list of message numbers.

What is backtrace in Rails?

The stack trace (usually named "backtrace" in Ruby, but also referred to as "stack backtrace" and "stack traceback") is a human-readable representation of the stack at a specific moment while running your program.

What is meant by stack traces?

In computing, a stack trace (also called stack backtrace or stack traceback) is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places; the stack and the heap.


1 Answers

The important thing is that rails console use irb, and has access to the range of irb config options

$ rails c
Loading development environment (Rails 4.2.0)
>> conf
=> conf.ap_name="irb"
conf.auto_indent_mode=false
conf.back_trace_limit=16
.
.
.

And there it is: conf.back_trace_limit. So:

conf.back_trace_limit = 0

will effectively disable the backtrace for the current session, and output will be nice and concise:

>> MyModel.gnu
NoMethodError: undefined method `gnu' for MyModel:Class

or

>> obj.do_defective_math
ZeroDivisionError: divided by 0

To make things a bit more convenient, a function can be defined in ~/.irbrc. Something like:

def toggle_trace
  if conf.back_trace_limit > 0
    conf.back_trace_limit = 0
  else
    conf.back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
  end
end

which can be called a console session to disable or enable the back trace as needed

like image 135
Ian Avatar answered Sep 18 '22 12:09

Ian