How to catch Errno::ECONNRESET class in "case when"?
This is because of how the ===
operator works on the class Class
The case
statement internally calls the ===
method on the object you are evaluating against. If you want to test for e
class, you just test against e
, not e.class
. That's because e.class
would fall into the when Class
case, because, well, e.class is a Class.
rescue Exception => e case e when Errno::ECONNRESET p 1 when Errno::ECONNRESET,Errno::ECONNABORTED,Errno::ETIMEDOUT p 2 else p 3 endend
Yeah, Ruby can have weird semantics sometimes
Well it depends upon whether you referencing the class or the constant.I have for instance had to use the following case statement to get a certain type of detection working
def fail(exception_error)exception = exception_errorcase exception.class when /HTTPClient::ConnectTimeoutError.new/ status = 'CONNECTION TIMEOUT' connection_status = 'DOWN' else status = 'UNKNOWN FAILURE' connection_status = 'DOWN'end
But that's because I'm working with the actual Exception Class not the constant.HTTPCLient is raising an actual class object:
class TimeoutError < RuntimeErrorend class ConnectTimeoutError < TimeoutErrorend
Here's a puzzling fact:
error = HTTPClient::ConnectTimeoutError.newHTTPClient::ConnectTimeoutError === error#=> trueerror === HTTPClient::ConnectTimeoutError#=> false
Not sure what to make of that.