From aa9b3133cde8761b75a41c28be107e1bb9099212 Mon Sep 17 00:00:00 2001 From: Nicholas Lee Date: Mon, 15 Jan 2024 18:21:18 -0800 Subject: [PATCH] Adds specific errors for connection timeout and failure 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 --- lib/plaid/api_client.rb | 7 +++-- templates/ruby/api_client.mustache | 5 +++ .../ruby/api_client_faraday_partial.mustache | 4 +-- test/test_api_client.rb | 31 +++++++++++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/plaid/api_client.rb b/lib/plaid/api_client.rb index bea8b35d3..2340aedca 100644 --- a/lib/plaid/api_client.rb +++ b/lib/plaid/api_client.rb @@ -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 @@ -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] diff --git a/templates/ruby/api_client.mustache b/templates/ruby/api_client.mustache index a231ff51a..2e4588e44 100644 --- a/templates/ruby/api_client.mustache +++ b/templates/ruby/api_client.mustache @@ -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 diff --git a/templates/ruby/api_client_faraday_partial.mustache b/templates/ruby/api_client_faraday_partial.mustache index 06d9a4de8..5a337ab94 100644 --- a/templates/ruby/api_client_faraday_partial.mustache +++ b/templates/ruby/api_client_faraday_partial.mustache @@ -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] diff --git a/test/test_api_client.rb b/test/test_api_client.rb index 4c8cb60c6..5aea163ad 100644 --- a/test/test_api_client.rb +++ b/test/test_api_client.rb @@ -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"] @@ -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