What’s the best way to rescue exceptions from Net::HTTP?
Exceptions thrown are described in Ruby’s socket.c
, like Errno::ETIMEDOUT
, Errno::ECONNRESET
, and Errno::ECONNREFUSED
. The base class to all of these is SystemCallError
, but it feels weird to write code like the following because SystemCallError
seems so far removed from making an HTTP
call:
begin
response = Net::HTTP.get_response(uri)
response.code == "200"
rescue SystemCallError
false
end
Is it just me? Is there a better way to handle this beyond fixing Net::HTTP
to handle the Errno
exceptions that would likely pop up and encapsulate them in a parent HttpRequestException
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…