From 4420179f75e5dd7935c3576fd51850e52f610c01 Mon Sep 17 00:00:00 2001 From: staroval Date: Wed, 31 Jul 2024 17:37:20 +0300 Subject: [PATCH] Added new options error_wrapper for client --- lib/ezclient/client.rb | 1 + lib/ezclient/request.rb | 6 +++++- spec/ezclient_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/ezclient/client.rb b/lib/ezclient/client.rb index bb2ed7d..3e86280 100644 --- a/lib/ezclient/client.rb +++ b/lib/ezclient/client.rb @@ -15,6 +15,7 @@ class EzClient::Client ssl_context timeout follow + error_wrapper ].freeze def initialize(options = {}) diff --git a/lib/ezclient/request.rb b/lib/ezclient/request.rb index 35ab5e4..ee28885 100644 --- a/lib/ezclient/request.rb +++ b/lib/ezclient/request.rb @@ -28,7 +28,7 @@ def perform end rescue => error on_error.call(self, error, options[:metadata]) - raise error + error_wrapper.call(self, error, options[:metadata]) end def perform! @@ -174,6 +174,10 @@ def follow options[:follow].is_a?(Hash) ? options[:follow] : {} end + def error_wrapper + options[:error_wrapper] || proc { |_request, error, _metadata| raise error } + end + def prepare_headers(headers) headers = HTTP::Headers.coerce(headers) headers[:user_agent] ||= "ezclient/#{EzClient::VERSION}" diff --git a/spec/ezclient_spec.rb b/spec/ezclient_spec.rb index 10c08dc..bfd291a 100644 --- a/spec/ezclient_spec.rb +++ b/spec/ezclient_spec.rb @@ -271,6 +271,26 @@ def self.sign!(*); end expect(calls.size).to eq(1) end end + + context "when error_wrapper callback is provided" do + let(:client_options) { { error_wrapper: error_wrapper } } + let(:calls) { [] } + + let(:error_wrapper) do + proc do |request, error, _metadata| + expect(request.url).to eq("http://example.com") + expect(request.elapsed_seconds).to be_a(Float) + expect(error).to be_a(StandardError) + calls << nil + raise "Wrapped some error" + end + end + + it "calls the error_wrapper callback" do + expect { request.perform }.to raise_error("Wrapped some error") + expect(calls.size).to eq(1) + end + end end context "when connection exception occurs" do