From d5b87e4d633bc153fea0340da0bdad4a597bf82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Gyebn=C3=A1r?= Date: Tue, 10 Aug 2021 16:34:24 +0200 Subject: [PATCH 1/2] Displays server response when introspection failed --- graphql_client_cli/src/introspect_schema.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/graphql_client_cli/src/introspect_schema.rs b/graphql_client_cli/src/introspect_schema.rs index 0791ee8a..0e4cf5e6 100644 --- a/graphql_client_cli/src/introspect_schema.rs +++ b/graphql_client_cli/src/introspect_schema.rs @@ -1,3 +1,4 @@ +use crate::error::Error; use crate::CliResult; use graphql_client::GraphQLQuery; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE}; @@ -53,9 +54,14 @@ pub fn introspect_schema( if res.status().is_success() { // do nothing } else if res.status().is_server_error() { - println!("server error!"); + return Err(Error::message("server error!".into())); } else { - println!("Something else happened. Status: {:?}", res.status()); + let status = res.status(); + let error_message = match res.text() { + Ok(msg) => format!("HTTP {}: {}", status, msg), + Err(_) => format!("HTTP {}", status), + }; + return Err(Error::message(error_message)); } let json: serde_json::Value = res.json()?; From 0e330815a1adab2e790626c34fafec4f12ead177 Mon Sep 17 00:00:00 2001 From: aedm Date: Mon, 13 Sep 2021 21:55:42 +0200 Subject: [PATCH 2/2] Pretty JSON error responses --- graphql_client_cli/src/introspect_schema.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/graphql_client_cli/src/introspect_schema.rs b/graphql_client_cli/src/introspect_schema.rs index 0e4cf5e6..36a0f8f5 100644 --- a/graphql_client_cli/src/introspect_schema.rs +++ b/graphql_client_cli/src/introspect_schema.rs @@ -58,7 +58,10 @@ pub fn introspect_schema( } else { let status = res.status(); let error_message = match res.text() { - Ok(msg) => format!("HTTP {}: {}", status, msg), + Ok(msg) => match serde_json::from_str::(&msg) { + Ok(json) => format!("HTTP {}\n{}", status, serde_json::to_string_pretty(&json)?), + Err(_) => format!("HTTP {}: {}", status, msg), + }, Err(_) => format!("HTTP {}", status), }; return Err(Error::message(error_message));