Everything is ruby is a method. Ruby methods are as of Human Language such as, merge, select, sort, map etc., which makes ruby one of the most used language today. It’s also difficult to debug
in ruby.
What is Standard Error…?
The big daddy of all Ruby exceptions, the StandardError.
StandardError is a superclass
with many exception subclasses of its own, but like all errors, it descends from the Exception superclass. StandardErrors occur anytime a rescue
clause catches an exception without an explicit Exception class specified.
Types of Errors and how to resolve them.
1. NoMethodError
undefined method `grade' for nil:NilClass undefined method `<<' for {}:Hash
When a method called for a particular object is not present, the above error is thrown. The most common is nil:Nilclass
[1] pry(main)> nil.grade NoMethodError: undefined method `grade' for nil:NilClass [2] pry(main)>
Use try
method for solving nil class errors. try
returns nil in any case a method is called for nil object.
[2] pry(main)> nil.try(:something) => nil [3] pry(main)>
2. NoMethodError (for private method)
private method `resume' called for #Student:0x007ff2f69a0218
The above happens when we try to access private
method of a class from another class.
If we need to access a method of inherited class then use protected
and access a method of different class make it public
method.
3. NameError
undefined local variable or method `grade' for # uninitialized constant Report
The above error occurs when a variable is not initialised. This is similar to unknown variable error
in C or C++.
You know, it's simple to resolve, just initialise it. grade = 0 or grade = "".
4. ActiveRecord::RecordNotFound
Couldn\'t find id=45
When the row or record having a particular 'id' is not present in database. This usually occurs when we use Student.find(params[:id])
.
We can use find_by
and where
which never throws exception, but returns nil, when record is not found in the database.
# does not throw exception [2] pry(main)> Student.find_by_id(params[:id]) => nil [3] pry(main)> Student.find_by(id: params[:id]) => nil [4] pry(main)> Student.where(id: params[:id]).first => nil
5. TypeError
String can't be coerced into Fixnum
no implicit conversion of String into Integer
Say, we add a string and a number like "1" + 2
, TypeError occurs. There are String, FixNum, Array, Hash, Float datatypes.
Convert the object into same datatypes to perform any action.
[3] pry(main)> "1" + 2 TypeError: no implicit conversion of Fixnum into String from (pry):3:in `+' [4] pry(main)> "1".to_i + 2 => 3 [5] pry(main)>
6. ArgumentError
wrong number of arguments (0 for 1)
It's simple, Arguments of calling and called method dont't match.
my_grade = student(college, grade) def student(college) # Argument error end def student(college, grade = 0) # works fine as grade is assigned 0. end
7. ActionView::Template::Error
The above occurs when there is no template for a particular action in a controller and there is no redirects
to any other path or action.
Redirecting to required path or adding the required template for that particular action would solve the issue.
8. Routing Error
No route matches [GET] "/students"
You are accessing a wrong url or there has been no route initialised in config/routes
.
Use rake routes
to find out all the routes in the app.
9. Syntax Error
Most famous error of all in every language 🙂 . Look into ""
(quotes) or a ,
(comma). It would be missing.
10. LoadError
Could not load 'active_record/connection_adapters/_adapter'.
This is not allow server to begin as there is no adapter in config/database.yml
for connecting to database, it's a configuration issue.
Make sure that the adapter in config/database.yml is valid. mysql2, postgresql or sqlite3 and adding necessary adapter in the Gemfile.
11. URI::InvalidURIError (bad URI(is not URI?): ):
The above occurs when a URL is invalid or http://
or https://
is missing in the URL. This is most likely to happen in ENV
files.
Check the URL, add http:// or https:// as required.
12. Net::ReadTimeout
When we make a API call
a third party server from our app and the third party server does not respond, Net::ReadTimeout occurs.
Having a timeout for the API call is the solution. Use HTTParty
for making a API call, I recommend this gem to use 🙂
13. ActiveRecord::PendingMigrationError
Especially for rails newbies, when there is a migration is added and it doesn't reflect in the schema, then run rake db:migrate
.
This would solve the above. Note that this occurs only in DEV environment, likely to be suppressed in Production.
14. RunTimeError
Server cannot run because of some blockers such as Configuration issues or Syntax errors.
Depends on the scenario to debug.
Exception handling in ruby
Use rescue
to handle exceptions and give a definite response.
def some_method puts "no exceptions till here" # something went wrong rescue => e puts "something went wrong" { status => "error", "message" => "#{e}" } end