How to check for a specific salvage offer 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 causes errors, for example:

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 to add to another rescue all errors that are similar to the scheme http does not accept registry part ?

Since I want to do something different than just print an error and return false in this case.

+4
source share
3 answers

It depends.

I see that the three descriptions of exceptions are different. Are there types of exceptions?

If 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 types of exceptions are the same, you will still have a single salvation block, but you will decide what to do based on the 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 it help?

+12
source

Check what the exception class is if the http scheme does not accept part of the registry (you can do this with puts e.class ). I guess it will be different than 'Operation Timeout' - connect (2) '

then

 begin . rescue YourExceptionClass => e . rescue => e . end 
+4
source

Important Note. Wildcard Rescue, Default is StandardError. This will not save every mistake.

For example, SignalException: SIGTERM will not be saved with rescue => error . You will need to specifically use rescue SignalException => e .

0
source

Source: https://habr.com/ru/post/1383739/


All Articles