Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: log on unsuccessful responses #45

Merged
merged 17 commits into from
Dec 21, 2024
Merged
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
al-yanna marked this conversation as resolved.
Show resolved Hide resolved
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
Loading