Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for specific rescue clause for error handling in Rails 3.x?

I have the following code:

begin
  site = RedirectFollower.new(url).resolve
rescue => e
  puts e.to_s
  return false
end

Which throws errors like:

the scheme http does not accept registry part: www.officedepot.com;

the scheme http does not accept registry part: ww2.google.com/something;

Operation timed out - connect(2)

How can I add in another rescue for all errors that are like the scheme http does not accept registry part?

Since I want to do something other than just printing the error and returning false in that case.

like image 547
user1049097 Avatar asked Nov 29 '11 21:11

user1049097


People also ask

How do you rescue errors in Ruby?

A raised exception can be rescued to prevent it from crashing your application once it reaches the top of the call stack. In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from.

What happens if in a begin rescue block the rescue code has an error?

The code between “begin” and “rescue” is where a probable exception might occur. If an exception occurs, the rescue block will execute. You should try to be specific about what exception you're rescuing because it's considered a bad practice to capture all exceptions.

How do you catch errors in Rails?

Exception handling in Ruby on Rails using rescue_from We can use rescue_from to catch the exception. The rescue_from directive is an exception handler that rescues the specified exceptions raised within controller actions and reacts to those exceptions with a defined method.

Can we use rescue without begin?

The method definition itself does the work of begin , so you can omit it. You can also do this with blocks. Now, there is one more way to use the rescue keyword without begin .


2 Answers

That depends.

I see the three exception descriptions are different. Are the Exception types different as well?

If So you could write your code like this:

begin
  site = RedirectFollower.new(url).resolve
rescue ExceptionType1 => e
  #do something with exception that throws 'scheme http does not...'
else
  #do something with other exceptions
end

If the exception types are the same then you'll still have a single rescue block but will decide what to do based on a regular expression. Perhaps something like:

begin
  site = RedirectFollower.new(url).resolve
rescue Exception => e
  if e.message =~ /the scheme http does not accept registry part/
      #do something with it
  end
end

Does this help?

like image 58
leonardoborges Avatar answered Sep 27 '22 17:09

leonardoborges


Check what is exception class in case of 'the scheme http does not accept registry part' ( you can do this by puts e.class). I assume that this will be other than 'Operation timed out - connect(2)'

then:

begin
  .
rescue YourExceptionClass => e
  .
rescue => e
  .
end
like image 35
sparrovv Avatar answered Sep 27 '22 19:09

sparrovv