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
16 changes: 16 additions & 0 deletions lib/graphql-hive/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def send(path, body, _log_type)
http = setup_http(uri)
request = build_request(uri, body)
response = http.request(request)
@options[:logger].info("Response: #{response.code} - #{response.message}")
al-yanna marked this conversation as resolved.
Show resolved Hide resolved

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}"
extract_error_details(response)&.then { |details| error_message += ": [#{details}]" }
al-yanna marked this conversation as resolved.
Show resolved Hide resolved
@options[:logger].warn(error_message)
end

@options[:logger].debug(response.inspect)
@options[:logger].debug(response.body.inspect)
Expand All @@ -48,6 +56,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(", ")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"path" is the location of the error within the JSON structure of the request.

for example, if there is an error in the metadata: client property, it will be

"path": "/operations/0/metadata/client"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Just wondering what the "error.message" looks like in the body? Just wondering if that is enough info to debug.

Knowing the field and reason would be huge though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would look something like this, depending on the error:
image

rescue JSON::ParserError
nil
al-yanna marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 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
Loading