diff --git a/CHANGELOG.md b/CHANGELOG.md index 336bb0f7..79b779f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [3.8.2](https://github.com/algolia/algoliasearch-client-ruby/compare/3.8.1...3.8.2) + +- [f97e44ce0](https://github.com/algolia/api-clients-automation/commit/f97e44ce0) fix(cts): add tests for HTML error ([#4097](https://github.com/algolia/api-clients-automation/pull/4097)) by [@millotp](https://github.com/millotp/) + ## [3.8.1](https://github.com/algolia/algoliasearch-client-ruby/compare/3.8.0...3.8.1) - [36d583e35](https://github.com/algolia/api-clients-automation/commit/36d583e35) fix(specs): make the searchParams compatible with v4 ([#4108](https://github.com/algolia/api-clients-automation/pull/4108)) by [@millotp](https://github.com/millotp/) diff --git a/Gemfile.lock b/Gemfile.lock index 01002f0c..43385c7e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - algolia (3.8.1) + algolia (3.8.2) base64 (>= 0.2.0, < 1) faraday (>= 1.0.1, < 3.0) faraday-net_http_persistent (>= 0.15, < 3) diff --git a/lib/algolia/error.rb b/lib/algolia/error.rb index 7a744e90..1b03741d 100644 --- a/lib/algolia/error.rb +++ b/lib/algolia/error.rb @@ -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 diff --git a/lib/algolia/transport/http/http_requester.rb b/lib/algolia/transport/http/http_requester.rb index 318b0e69..80d5a565 100644 --- a/lib/algolia/transport/http/http_requester.rb +++ b/lib/algolia/transport/http/http_requester.rb @@ -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) diff --git a/lib/algolia/transport/http/response.rb b/lib/algolia/transport/http/response.rb index fb8c878c..cc6dea15 100644 --- a/lib/algolia/transport/http/response.rb +++ b/lib/algolia/transport/http/response.rb @@ -1,13 +1,14 @@ 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 @@ -15,6 +16,7 @@ class Response # def initialize(opts = {}) @status = opts[:status] + @reason_phrase = opts[:reason_phrase] @body = opts[:body] @error = opts[:error] || "" @headers = opts[:headers] || "" diff --git a/lib/algolia/transport/transport.rb b/lib/algolia/transport/transport.rb index 14c7e416..8c190b88 100644 --- a/lib/algolia/transport/transport.rb +++ b/lib/algolia/transport/transport.rb @@ -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 diff --git a/lib/algolia/version.rb b/lib/algolia/version.rb index 680549fc..ea0acba0 100644 --- a/lib/algolia/version.rb +++ b/lib/algolia/version.rb @@ -1,5 +1,5 @@ # Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT. module Algolia - VERSION = "3.8.1".freeze + VERSION = "3.8.2".freeze end