Skip to content

Commit

Permalink
fix: log on unsuccessful responses (#45)
Browse files Browse the repository at this point in the history
* log on unnsuccesful responses

* lint

* bump to v0.5.3

* fix: refactor code

Co-authored-by: Arya Bhimani <[email protected]>

* 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 <[email protected]>
  • Loading branch information
al-yanna and aryascripts authored Dec 21, 2024
1 parent 980ea7d commit 6313562
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
graphql-hive (0.5.3)
graphql-hive (0.5.4)
graphql (>= 2.3, < 3)

GEM
Expand Down
14 changes: 14 additions & 0 deletions lib/graphql-hive/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/graphql-hive/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Graphql
module Hive
VERSION = "0.5.3"
VERSION = "0.5.4"
end
end
40 changes: 39 additions & 1 deletion spec/graphql/graphql-hive/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 6313562

Please sign in to comment.