Skip to content

Commit

Permalink
Added new options error_wrapper for client
Browse files Browse the repository at this point in the history
  • Loading branch information
staroval committed Jul 31, 2024
1 parent 3221ad6 commit 4420179
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/ezclient/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EzClient::Client
ssl_context
timeout
follow
error_wrapper
].freeze

def initialize(options = {})
Expand Down
6 changes: 5 additions & 1 deletion lib/ezclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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}"
Expand Down
20 changes: 20 additions & 0 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4420179

Please sign in to comment.