Skip to content

Commit

Permalink
log on unnsuccesful responses
Browse files Browse the repository at this point in the history
  • Loading branch information
al-yanna committed Dec 4, 2024
1 parent 938e7ce commit 62cf884
Showing 1 changed file with 14 additions and 0 deletions.
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)

if response.code.to_i >= 400 && response.code.to_i < 500
error_message = "Unsuccessful response: #{response.code} - #{response.message}"
extract_error_details(response)&.then { |details| error_message += ": [#{details}]" }
@options[:logger].warn(error_message)
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)

Check failure on line 60 in lib/graphql-hive/client.rb

View workflow job for this annotation

GitHub Actions / standard

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
parsed_body['errors'].map { |error| "path: #{error['path']}, message: #{error['message']}" }.join(", ")

Check failure on line 61 in lib/graphql-hive/client.rb

View workflow job for this annotation

GitHub Actions / standard

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 61 in lib/graphql-hive/client.rb

View workflow job for this annotation

GitHub Actions / standard

Style/StringLiteralsInInterpolation: Prefer double-quoted strings inside interpolations.

Check failure on line 61 in lib/graphql-hive/client.rb

View workflow job for this annotation

GitHub Actions / standard

Style/StringLiteralsInInterpolation: Prefer double-quoted strings inside interpolations.
rescue JSON::ParserError
nil
end
end
end
end

0 comments on commit 62cf884

Please sign in to comment.