Skip to content

Commit

Permalink
Added new options error_wrapper for client options (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
AS-AlStar authored Jul 31, 2024
1 parent 3221ad6 commit e6806b9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Valid client options are:
- `ssl_context` – ssl context for requests (an `OpenSSL::SSL::SSLContext` instance)
- `timeout` – timeout for requests in seconds or hash like `{ read: 5, write: 5, connect: 1 }`
- `follow` – enable following redirects (`true` or hash with options – e.g. `{ max_hops: 1, strict: false}`)
- `error_wrapper` – callback called on request exception, makes it possible to handle any error, default behavior: `raise error`

All these options are passed to each request made by this client but can be overriden on per-request basis.

Expand Down Expand Up @@ -91,13 +92,15 @@ You can provide `on_complete`, `on_error` and `on_retry` callbacks like this:
on_complete = -> (request, response, metadata) { ... }
on_error = -> (request, error, metadata) { ... }
on_retry = -> (request, error, metadata) { ... }
error_wrapper = -> (request, error, metadata) { raise error }

client = EzClient.new(
on_complete: on_complete,
on_error: on_error,
on_retry: on_retry,
retry_exceptions: [StandardError],
max_retries: 2,
error_wrapper: error_wrapper
)

response = client.perform!(:get, url, metadata: :hello)
Expand Down
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 e6806b9

Please sign in to comment.