From 62cf88485b02aabbdc724fc29a63695bcb27a578 Mon Sep 17 00:00:00 2001 From: Alyanna Santos Date: Wed, 4 Dec 2024 16:26:58 -0500 Subject: [PATCH] log on unnsuccesful responses --- lib/graphql-hive/client.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/graphql-hive/client.rb b/lib/graphql-hive/client.rb index e54c53d..9e077d0 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) + 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 @@ -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 + nil + end end end end