Skip to content

Commit

Permalink
Replace Faraday::Error::* usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkim committed Apr 5, 2024
1 parent 8b7db83 commit 8969f3d
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/elastomer_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def request(method, path, params)
handle_errors response

# wrap Faraday errors with appropriate ElastomerClient::Client error classes
rescue Faraday::Error::ClientError => boom
rescue Faraday::ClientError, Faraday::ServerError => boom
error = wrap_faraday_error(boom, method, path)
raise error
rescue OpaqueIdError => boom
Expand Down
11 changes: 0 additions & 11 deletions lib/elastomer_client/client/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ def fatal
ConnectionFailed.fatal = false
RejectedExecutionError.fatal = false

# Define an ElastomerClient::Client exception class on the fly for
# Faraday exception classes that we don't specifically wrap.
Faraday::Error.constants.each do |error_name|
next if ::ElastomerClient::Client.const_get(error_name) rescue nil

error_class = Faraday::Error.const_get(error_name)
next unless error_class < Faraday::Error::ClientError

::ElastomerClient::Client.const_set(error_name, Class.new(Error))
end

# Exception for operations that are unsupported with the version of
# Elasticsearch being used.
IncompatibleVersionException = Class.new Error
Expand Down
2 changes: 1 addition & 1 deletion lib/elastomer_client/middleware/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(environment)
def parse(body)
MultiJson.load(body) if body.respond_to?(:to_str) && !body.strip.empty?
rescue StandardError, SyntaxError => e
raise Faraday::Error::ParsingError, e
raise Faraday::ParsingError, e
end

def process_response?(env)
Expand Down
2 changes: 1 addition & 1 deletion test/client/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
end

it "is instantiated from another exception" do
err = Faraday::Error::ConnectionFailed.new "could not connect to host"
err = Faraday::ConnectionFailed.new "could not connect to host"
err.set_backtrace %w[one two three four]

err = ElastomerClient::Client::Error.new(err, "POST", "/index/doc")
Expand Down
12 changes: 6 additions & 6 deletions test/middleware/parse_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def process(body, content_type = nil)
end

it "chokes on invalid json" do
assert_raises(Faraday::Error::ParsingError) { process "{!" }
assert_raises(Faraday::Error::ParsingError) { process "invalid" }
assert_raises(Faraday::ParsingError) { process "{!" }
assert_raises(Faraday::ParsingError) { process "invalid" }

# surprisingly these are all valid according to MultiJson
#
# assert_raises(Faraday::Error::ParsingError) { process '"a"' }
# assert_raises(Faraday::Error::ParsingError) { process 'true' }
# assert_raises(Faraday::Error::ParsingError) { process 'null' }
# assert_raises(Faraday::Error::ParsingError) { process '1' }
# assert_raises(Faraday::ParsingError) { process '"a"' }
# assert_raises(Faraday::ParsingError) { process 'true' }
# assert_raises(Faraday::ParsingError) { process 'null' }
# assert_raises(Faraday::ParsingError) { process '1' }
end
end
2 changes: 1 addition & 1 deletion test/notifications_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

it "instruments timeouts" do
$client.stub :connection, lambda { raise Faraday::Error::TimeoutError } do
$client.stub :connection, lambda { raise Faraday::TimeoutError } do
assert_raises(ElastomerClient::Client::TimeoutError) { $client.info }
event = @events.detect { |e| e.payload[:action] == "cluster.info" }
exception = event.payload[:exception]
Expand Down

0 comments on commit 8969f3d

Please sign in to comment.