# frozen_string_literal: true module EvilMartiansAPI module Middleware # Handle connection errors in the context of the Faraday underlying HTTP client library. # # @raise [Errors::ConnectionError] Raises a custom ConnectionError. # class HandleConnectionErrorMiddleware < Faraday::Middleware FARADAY_CONNECTION_ERRORS = [ Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::SSLError, ].freeze def call(env) @app.call(env) rescue *FARADAY_CONNECTION_ERRORS => e raise Errors::ConnectionError, e end Faraday::Middleware.register_middleware( evil_martians_api_handle_connection_error: self, ) end end end