Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created September 6, 2019 08:57
Show Gist options
  • Save RStankov/7b89704f10f4442def47551756cd7ac3 to your computer and use it in GitHub Desktop.
Save RStankov/7b89704f10f4442def47551756cd7ac3 to your computer and use it in GitHub Desktop.
# 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