Skip to content

Commit aad41b4

Browse files
algolia-botmillotp
andcommitted
fix(cts): add tests for HTML error (generated)
algolia/api-clients-automation#4097 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 620afbe commit aad41b4

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

lib/algolia/error.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def initialize(message, errors = [])
2727
# which is also included in the response attribute.
2828
#
2929
class AlgoliaHttpError < AlgoliaError
30-
attr_accessor :code, :message
30+
attr_accessor :code, :http_message
3131

3232
def initialize(code, message)
3333
self.code = code
34-
self.message = message
35-
super("#{self.code()}: #{self.message()}")
34+
self.http_message = message
35+
super("#{code}: #{message}")
3636
end
3737
end
3838
end

lib/algolia/transport/http/http_requester.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,24 @@ def send_request(host, method, path, body, query_params, headers, timeout, conne
3939
@logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}")
4040
end
4141

42-
return Http::Response.new(status: response.status, body: response.body, headers: response.headers)
42+
return Http::Response.new(
43+
status: response.status,
44+
reason_phrase: response.reason_phrase,
45+
body: response.body,
46+
headers: response.headers
47+
)
4348
end
4449

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

49-
Http::Response.new(status: response.status, error: response.body, headers: response.headers)
54+
Http::Response.new(
55+
status: response.status,
56+
reason_phrase: response.reason_phrase,
57+
error: response.body,
58+
headers: response.headers
59+
)
5060
rescue Faraday::TimeoutError => e
5161
@logger.info("Request timed out. Error: #{e.message}") if ENV["ALGOLIA_DEBUG"]
5262
Http::Response.new(error: e.message, has_timed_out: true)

lib/algolia/transport/http/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
module Algolia
22
module Http
33
class Response
4-
attr_reader :status, :body, :error, :headers, :has_timed_out, :network_failure
4+
attr_reader :status, :reason_phrase, :body, :error, :headers, :has_timed_out, :network_failure
55

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

99
#
1010
# @option status [String] Response status
11+
# @option reason_phrase [String] Response reason phrase
1112
# @option body [String] Response body
1213
# @option error [String] Response error or caught error
1314
# @option headers [String] Response headers
1415
# @option has_timed_out [String] If the request has timed out
1516
#
1617
def initialize(opts = {})
1718
@status = opts[:status]
19+
@reason_phrase = opts[:reason_phrase]
1820
@body = opts[:body]
1921
@error = opts[:error] || ""
2022
@headers = opts[:headers] || ""

lib/algolia/transport/transport.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def request(call_type, method, path, body, opts = {})
6969
network_failure: response.network_failure
7070
)
7171
if outcome == FAILURE
72+
# handle HTML error
73+
if response.headers["content-type"]&.include?("text/html")
74+
raise Algolia::AlgoliaHttpError.new(response.status, response.reason_phrase)
75+
end
76+
7277
decoded_error = JSON.parse(response.error, :symbolize_names => true)
7378
raise Algolia::AlgoliaHttpError.new(response.status, decoded_error[:message])
7479
end

0 commit comments

Comments
 (0)