From c9a2a7841ca3de61a3686e84c133329473f18a9a Mon Sep 17 00:00:00 2001 From: Nicholas Lee Date: Mon, 15 Jan 2024 18:21:18 -0800 Subject: [PATCH 1/2] 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 796867bd5..b5cbe2940 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 From 5286788f2d5b705933b1967e166bbe0cfd2b1ce5 Mon Sep 17 00:00:00 2001 From: Nicholas Lee Date: Tue, 20 Feb 2024 09:45:18 -0800 Subject: [PATCH 2/2] Ensure `api_error` is loaded in `api_client` Require default gems for test suite Fix `MiniTest` namespace issue --- lib/plaid/api_client.rb | 1 + test/test_helper.rb | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/plaid/api_client.rb b/lib/plaid/api_client.rb index b5cbe2940..f5327f398 100644 --- a/lib/plaid/api_client.rb +++ b/lib/plaid/api_client.rb @@ -20,6 +20,7 @@ require 'tempfile' require 'faraday' require 'faraday/multipart' +require_relative 'api_error' module Plaid class ApiClient diff --git a/test/test_helper.rb b/test/test_helper.rb index d3772831c..57c8f284f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,13 +1,22 @@ +require 'bundler' + +begin + Bundler.setup(:default, :development) +rescue Bundler::BundlerError => e + warn e.message + warn 'Run `bundle install` to install missing gems' + exit e.status_code +end + +require "plaid" require "minitest/autorun" require "minitest/around/unit" require "json" -require_relative "../lib/plaid" - # Internal: Default read timeout for HTTP calls in seconds. NETWORK_TIMEOUT = 600 -class PlaidTest < MiniTest::Test +class PlaidTest < Minitest::Test attr_reader :client, :item, :access_token def create_client