Skip to content

Commit

Permalink
fix(cts): add tests for HTML error (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#4097

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
algolia-bot and millotp committed Nov 19, 2024
1 parent 620afbe commit aad41b4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/algolia/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def initialize(message, errors = [])
# which is also included in the response attribute.
#
class AlgoliaHttpError < AlgoliaError
attr_accessor :code, :message
attr_accessor :code, :http_message

def initialize(code, message)
self.code = code
self.message = message
super("#{self.code()}: #{self.message()}")
self.http_message = message
super("#{code}: #{message}")
end
end
end
14 changes: 12 additions & 2 deletions lib/algolia/transport/http/http_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@ def send_request(host, method, path, body, query_params, headers, timeout, conne
@logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}")
end

return Http::Response.new(status: response.status, body: response.body, headers: response.headers)
return Http::Response.new(
status: response.status,
reason_phrase: response.reason_phrase,
body: response.body,
headers: response.headers
)
end

if ENV["ALGOLIA_DEBUG"]
@logger.info("Request failed. Response status: #{response.status}, error: #{response.body}")
end

Http::Response.new(status: response.status, error: response.body, headers: response.headers)
Http::Response.new(
status: response.status,
reason_phrase: response.reason_phrase,
error: response.body,
headers: response.headers
)
rescue Faraday::TimeoutError => e
@logger.info("Request timed out. Error: #{e.message}") if ENV["ALGOLIA_DEBUG"]
Http::Response.new(error: e.message, has_timed_out: true)
Expand Down
4 changes: 3 additions & 1 deletion lib/algolia/transport/http/response.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module Algolia
module Http
class Response
attr_reader :status, :body, :error, :headers, :has_timed_out, :network_failure
attr_reader :status, :reason_phrase, :body, :error, :headers, :has_timed_out, :network_failure

# used for the echo requester
attr_reader :method, :path, :query_params, :host, :timeout, :connect_timeout

#
# @option status [String] Response status
# @option reason_phrase [String] Response reason phrase
# @option body [String] Response body
# @option error [String] Response error or caught error
# @option headers [String] Response headers
# @option has_timed_out [String] If the request has timed out
#
def initialize(opts = {})
@status = opts[:status]
@reason_phrase = opts[:reason_phrase]
@body = opts[:body]
@error = opts[:error] || ""
@headers = opts[:headers] || ""
Expand Down
5 changes: 5 additions & 0 deletions lib/algolia/transport/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def request(call_type, method, path, body, opts = {})
network_failure: response.network_failure
)
if outcome == FAILURE
# handle HTML error
if response.headers["content-type"]&.include?("text/html")
raise Algolia::AlgoliaHttpError.new(response.status, response.reason_phrase)
end

decoded_error = JSON.parse(response.error, :symbolize_names => true)
raise Algolia::AlgoliaHttpError.new(response.status, decoded_error[:message])
end
Expand Down

0 comments on commit aad41b4

Please sign in to comment.