From 63135621cd08a259b761afb53cbdd69ad6bafa93 Mon Sep 17 00:00:00 2001 From: Alyanna Santos <72548456+al-yanna@users.noreply.github.com> Date: Sat, 21 Dec 2024 12:45:13 -0500 Subject: [PATCH] fix: log on unsuccessful responses (#45) * log on unnsuccesful responses * lint * bump to v0.5.3 * fix: refactor code Co-authored-by: Arya Bhimani * fix: temp * fix: v0.5.4' * fix: temp log for all responses * fix: client spec * fix: update logs to info * chore: revert back to debug * fix: remove temp log * feat: add unit tests * feat: revert * feat: refactor --------- Co-authored-by: Arya Bhimani --- Gemfile.lock | 2 +- lib/graphql-hive/client.rb | 14 +++++++++ lib/graphql-hive/version.rb | 2 +- spec/graphql/graphql-hive/client_spec.rb | 40 +++++++++++++++++++++++- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ec556a5..6e758f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - graphql-hive (0.5.3) + graphql-hive (0.5.4) graphql (>= 2.3, < 3) GEM diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index e54c53d..10dcbeb 100644 --- a/lib/graphql-hive/client.rb +++ b/lib/graphql-hive/client.rb @@ -24,6 +24,12 @@ def send(path, body, _log_type) request = build_request(uri, body) response = http.request(request) + code = response.code.to_i + if code >= 400 && code < 500 + error_message = "Unsuccessful response: #{response.code} - #{response.message}" + @options[:logger].warn("#{error_message} #{extract_error_details(response)}") + end + @options[:logger].debug(response.inspect) @options[:logger].debug(response.body.inspect) rescue => e @@ -48,6 +54,14 @@ def build_request(uri, body) request.body = JSON.generate(body) request end + + def extract_error_details(response) + parsed_body = JSON.parse(response.body) + return unless parsed_body.is_a?(Hash) && parsed_body["errors"].is_a?(Array) + parsed_body["errors"].map { |error| "{ path: #{error["path"]}, message: #{error["message"]} }" }.join(", ") + rescue JSON::ParserError + "Could not parse response from Hive" + end end end end diff --git a/lib/graphql-hive/version.rb b/lib/graphql-hive/version.rb index 42eef1c..0af475a 100644 --- a/lib/graphql-hive/version.rb +++ b/lib/graphql-hive/version.rb @@ -2,6 +2,6 @@ module Graphql module Hive - VERSION = "0.5.3" + VERSION = "0.5.4" end end diff --git a/spec/graphql/graphql-hive/client_spec.rb b/spec/graphql/graphql-hive/client_spec.rb index 18e8f7a..75c9def 100644 --- a/spec/graphql/graphql-hive/client_spec.rb +++ b/spec/graphql/graphql-hive/client_spec.rb @@ -25,7 +25,7 @@ describe "#send" do let(:http) { instance_double(Net::HTTP) } let(:request) { instance_double(Net::HTTP::Post) } - let(:response) { instance_double(Net::HTTPOK, body: "", code: "200") } + let(:response) { instance_double(Net::HTTPOK, body: "", code: "200", message: "OK") } before do allow(Net::HTTP).to receive(:new).and_return(http) @@ -70,5 +70,43 @@ expect(options[:logger]).to receive(:fatal).with("Failed to send data: Network error") expect { client.send(:"/usage", body, :usage) }.not_to raise_error(StandardError, "Network error") end + + context "when the response status code is between 400 and 499" do + let(:response) do + instance_double( + Net::HTTPClientError, + body: '{"errors":[{"path":"test1","message":"Error message 1"},{"path":"test2","message":"Error message 2"}]}', + code: "400", + message: "Bad Request" + ) + end + + before do + allow(http).to receive(:request).and_return(response) + end + + it "logs a warning with error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request { path: test1, message: Error message 1 }, { path: test2, message: Error message 2 }") + client.send(:"/usage", body, :usage) + end + + context "when the response body is not valid JSON" do + let(:response) { instance_double(Net::HTTPClientError, body: "Invalid JSON", code: "400", message: "Bad Request") } + + it "logs a warning without error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 400 - Bad Request Could not parse response from Hive") + client.send(:"/usage", body, :usage) + end + end + + context "when the response body does not contain errors" do + let(:response) { instance_double(Net::HTTPClientError, body: "{}", code: "401", message: "Unauthorized") } + + it "logs a warning without error details" do + expect(options[:logger]).to receive(:warn).with("Unsuccessful response: 401 - Unauthorized ") + client.send(:"/usage", body, :usage) + end + end + end end end