Skip to content

Commit

Permalink
Adds specific errors for connection timeout and failure
Browse files Browse the repository at this point in the history
This should be backwards compatible with the existing code as
the new errors inherit from ApiError and maintain the same
string signature of the previous errors
  • Loading branch information
Maimer committed Jan 16, 2024
1 parent 675ae2e commit aa9b313
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions lib/plaid/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

module Plaid
class ApiClient
class ApiTimeoutError < ApiError; end
class ApiConnectionFailedError < ApiError; end

# The Configuration object holding settings to be used in the API client.
attr_accessor :config

Expand Down Expand Up @@ -99,9 +102,9 @@ def call_api(http_method, path, opts = {})
end
end
rescue Faraday::TimeoutError
fail ApiError.new('Connection timed out')
fail ApiTimeoutError.new('Connection timed out')
rescue Faraday::ConnectionFailed
fail ApiError.new('Connection failed')
fail ApiConnectionFailedError.new('Connection failed')
end

if opts[:return_type]
Expand Down
5 changes: 5 additions & 0 deletions templates/ruby/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ require 'faraday/multipart'

module {{moduleName}}
class ApiClient
{{#isFaraday}}
class ApiTimeoutError < ApiError; end
class ApiConnectionFailedError < ApiError; end

{{/isFaraday}}
# The Configuration object holding settings to be used in the API client.
attr_accessor :config

Expand Down
4 changes: 2 additions & 2 deletions templates/ruby/api_client_faraday_partial.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
end
end
rescue Faraday::TimeoutError
fail ApiError.new('Connection timed out')
fail ApiTimeoutError.new('Connection timed out')
rescue Faraday::ConnectionFailed
fail ApiError.new('Connection failed')
fail ApiConnectionFailedError.new('Connection failed')
end

if opts[:return_type]
Expand Down
31 changes: 29 additions & 2 deletions test/test_api_client.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'faraday'
require 'faraday/multipart'


require_relative "test_helper"

# Internal: The test for API Client
class APIClientTest < PlaidTest
class ApiClientTest < PlaidTest
def test_api_client_exposes_farday_connection
configuration = Plaid::Configuration.new
configuration.server_index = Plaid::Configuration::Environment["sandbox"]
Expand All @@ -20,4 +19,32 @@ def test_api_client_exposes_farday_connection

assert_kind_of(Faraday::Connection, api_client.connection)
end

def test_timeout_error
api_client = Plaid::ApiClient.new
exception = -> { raise Faraday::TimeoutError.new }
options = { header_params: {} }

error = assert_raises Plaid::ApiClient::ApiTimeoutError do
api_client.connection.stub :get, exception do
api_client.call_api(:get, 'dummy_path', options)
end
end

assert_equal 'Connection timed out', error.message
end

def test_connection_error
api_client = Plaid::ApiClient.new
exception = -> { raise Faraday::ConnectionFailed.new }
options = { header_params: {} }

error = assert_raises Plaid::ApiClient::ApiConnectionFailedError do
api_client.connection.stub :get, exception do
api_client.call_api(:get, 'dummy_path', options)
end
end

assert_equal 'Connection failed', error.message
end
end

0 comments on commit aa9b313

Please sign in to comment.