Created
September 6, 2019 08:57
-
-
Save RStankov/7b89704f10f4442def47551756cd7ac3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Handle::NetworkErrors | |
extend self | |
ERRORS = [ | |
EOFError, | |
Errno::ECONNREFUSED, | |
Errno::ECONNRESET, | |
Errno::EFAULT, | |
Errno::EINVAL, | |
Errno::EMFILE, | |
Errno::ENETUNREACH, | |
Errno::EPIPE, | |
Errno::ETIMEDOUT, | |
HTTP::ConnectionError, | |
HTTParty::Error, | |
IOError, | |
JSON::ParserError, | |
Net::HTTPBadResponse, | |
Net::HTTPHeaderSyntaxError, | |
Net::OpenTimeout, | |
Net::ProtocolError, | |
Net::ReadTimeout, | |
OpenSSL::SSL::SSLError, | |
SocketError, | |
Timeout::Error, | |
Zlib::DataError, | |
].freeze | |
def ===(error) | |
ERRORS.any? { |error_class| error_class === error } # rubocop:disable Style/CaseEquality | |
end | |
def call(fallback:) | |
yield | |
rescue *ERRORS => _e | |
fallback | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment