[RAILS] About Ruby error messages

1. 1. Introduction

Beginners in programming inevitably take a long time to read error messages. For the time being, I tend to copy the full text of the quickest error message and google it. However, if you google, you will get some information like that, but you cannot say that it is the correct information. As pointed out in "Introduction to Ruby for Professionals", even though it seems that it was solved successfully by executing the googled result, it actually created a serious security flaw. It seems that there are times. So, first of all, I think it is necessary to be able to read the error message and be able to extract the problem by yourself. I wrote this article because I thought it would help me get rid of bugs and errors fairly quickly and understand the structure of Ruby.

Referenced → [Introduction to Ruby for those who aim to become professionals](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%92%E7% 9B% AE% E6% 8C% 87% E3% 81% 99% E4% BA% BA% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AERuby% E5% 85% A5% E9% 96% 80-% E8% A8% 80% E8% AA% 9E% E4% BB% 95% E6% A7% 98% E3% 81% 8B% E3% 82% 89% E3% 83% 86 % E3% 82% B9% E3% 83% 88% E9% A7% 86% E5% 8B% 95% E9% 96% 8B% E7% 99% BA% E3% 83% BB% E3% 83% 87% E3 % 83% 90% E3% 83% 83% E3% 82% B0% E6% 8A% 80% E6% B3% 95% E3% 81% BE% E3% 81% A7-Software-Design-plus% E3% 82 % B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 4774193976)

2. How to read the backtrace

If an error occurs during program execution, data showing the method call status is output. This is called a backtrace. Here, I dare to enter the wrong syntax and call the backtrace.

irb(main):001:0> puts hoge
Traceback (most recent call last):
        5: from /(Path where Ruby is installed)/irb:23:in `<main>'
        4: from /(Path where Ruby is installed)/irb:23:in `load'
        3: from /(Path where Ruby is installed)/irb:11:in `<top (required)>'
        2: from (irb):1
        1: from (irb):2:in `rescue in irb_binding'
NameError (undefined local variable or method `hoge' for main:Object)

The above is the backtrace output because I entered the wrong syntax puts hoge in irb (Ruby that can be executed on the terminal). This is a very simple error that occurred because hoge could not be recognized as a character string by enclosing it in single quotes (') or double quotes ("). Let's take a closer look at each one.


Traceback (most recent call last):

However, this is not an error message, but rather a traceback (the last latest call), that is, Ruby is executed and the latest execution process up to the error is displayed next. And then

        5: from /(Path where Ruby is installed)/irb:23:in `<main>'
        4: from /(Path where Ruby is installed)/irb:23:in `load'
        3: from /(Path where Ruby is installed)/irb:11:in `<top (required)>'
        2: from (irb):1
        1: from (irb):2:in `rescue in irb_binding'

However, the execution process up to the error is displayed in the flow from 5: to 1:. The lower you go, the closer you are to the error (in other words, the closer you are to the latest). Here, from 5: to 3:, it means "execution process of irb program", and in the following 2:, it means "execute the first line of irb". I checked 1: but didn't understand the meaning of rescue in irb_binding. Does that mean "is it supplemented by the second line of irb?" Next,

NameError (undefined local variable or method `hoge' for main:Object)

This is the error message. The first is NameError, which is the type of error message. The types of error messages will be explained later. The following is ʻundefined local variable or method hoge'for main: Object, but since hoge`` cannot be recognized as a string, it is searched from a local variable or constant, and it is not defined. It means that · · · This time it's a backtrace due to a simple mistake, so it only takes 5 lines, but if it gets complicated, you may see dozens of lines of execution. Even in such a case, it is necessary to read the contents calmly and carefully.

3. 3. Main error message types

Error name Overview
NameError Occurs when using undefined local variables or constants
NoMethodError Occurs when trying to call a method that does not exist
SyntaxError Occurs when there is a syntax error in the source code
TypeError Occurs when an object that is not the expected type or does not have the expected behavior is passed as a method argument
SystemStackError Occurs when the system stack overflows. Typically, it happens when you make a method call infinitely recursion.
LoadError Occurs when require or load fails

For other errors, see the Official Reference (https://docs.ruby-lang.org/en/2.7.0/library/_builtin.html).

4. What to do if you meet with an error message

(1) Check the procedure you performed and read the backtrace.

(2) Use a debugger.

(3) Try running a simple code with irb.

(4) Check the log.

(5) Read Official Documentation.

(6) Search for issue.

(7) Read the code of the external library.

(8) Ask someone.

5. Summary

As a beginner, seeing a lot of error messages can be quite discouraging. You need to be calm and solid in responding to error messages. In case you encounter an error, you may want to think about what to do in advance.

Recommended Posts

About Ruby error messages
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About No Method Error
About Ruby exception handling
[Ruby on Rails] How to display error messages
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
[Ruby on Rails] Individual display of error messages
Japanese localization of error messages
About Spring Framework context error
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
[Ruby] Introduction to Ruby Error statement
Ruby About various iterative processes
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
About the behavior of ruby Hash # ==
[Rails] Japanese localization of error messages
About error handling of comment function
About Invalid Request Error in Payjp
About Ruby single quotes and double quotes
[Rails] Display form error messages asynchronously
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
Japanese localization of error messages (rails)
Animated display of Swift error messages
About =
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About error when implementing spring validation
I want to eliminate duplicate error messages
[Ruby on Rails] About bundler (for beginners)
[Rails] How to display error messages individually
Testing for Error Messages: Rails Tutorial Notes-Chapter 7
[Ruby / Rails] Mechanism for retrying Thread Error
[Ruby on Rails] About Active Record callbacks
About the error message Invalid redeclaration of'***'
Tips for gRPC error handling in Ruby
How to display error messages in Japanese
[Ruby on Rails] Add and delete tags and display (success / error) messages using ajax.