From 1a86858a784218e4bf5baca381b79b79712b3f86 Mon Sep 17 00:00:00 2001 From: Irina Shestak Date: Tue, 11 May 2021 15:56:26 +0200 Subject: [PATCH] add error suggestions --- installers/npm/package-lock.json | 2 +- src/command/subgraph/check.rs | 14 +++++++------- src/command/subgraph/publish.rs | 22 +++++++++++++--------- src/error/metadata/suggestion.rs | 4 +++- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/installers/npm/package-lock.json b/installers/npm/package-lock.json index c7acb44c2..8981bd5d5 100644 --- a/installers/npm/package-lock.json +++ b/installers/npm/package-lock.json @@ -505,4 +505,4 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } -} \ No newline at end of file +} diff --git a/src/command/subgraph/check.rs b/src/command/subgraph/check.rs index 3cdc3db4f..185fe4663 100644 --- a/src/command/subgraph/check.rs +++ b/src/command/subgraph/check.rs @@ -2,8 +2,8 @@ use ansi_term::Colour::Red; use serde::Serialize; use structopt::StructOpt; -use crate::Result; -use rover_client::query::subgraph::check; +use crate::{anyhow, error::RoverError, Result, Suggestion}; +use rover_client::query::{config::is_federated, subgraph::check}; use crate::command::RoverStdout; use crate::utils::client::StudioClientConfig; @@ -76,13 +76,13 @@ impl Check { // We don't want to run subgraph check on a non-federated graph, so // return an error and recommend running graph check instead. - if !federated_response.result && !self.convert { - return Err(RoverError::new(anyhow!( - "Not able to run subgraph check on a non-federated graph." - ))); + if !federated_response.result { + let err = anyhow!("Not able to run subgraph check on a non-federated graph."); + let mut err = RoverError::new(err); + err.set_suggestion(Suggestion::UseFederatedGraph); + return Err(err); } - let partial_schema = check::check_partial_schema_query::PartialSchemaInput { sdl: Some(sdl), // we never need to send the hash since the back end computes it from SDL diff --git a/src/command/subgraph/publish.rs b/src/command/subgraph/publish.rs index 5ed613ece..35a341096 100644 --- a/src/command/subgraph/publish.rs +++ b/src/command/subgraph/publish.rs @@ -2,14 +2,17 @@ use ansi_term::Colour::{Cyan, Red, Yellow}; use serde::Serialize; use structopt::StructOpt; -use crate::utils::{ - client::StudioClientConfig, - git::GitContext, - loaders::load_schema_from_flag, - parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource}, -}; use crate::{anyhow, Result}; use crate::{command::RoverStdout, error::RoverError}; +use crate::{ + utils::{ + client::StudioClientConfig, + git::GitContext, + loaders::load_schema_from_flag, + parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource}, + }, + Suggestion, +}; use rover_client::query::{ config::is_federated, @@ -84,9 +87,10 @@ impl Publish { // Error here if no --convert flag is passed _and_ the current context // is non-federated. Add a suggestion to require a --convert flag. if !federated_response.result && !self.convert { - return Err(RoverError::new(anyhow!( - "Could not publish a subgraph to a non-federated graph." - ))); + let err = anyhow!("Could not publish a subgraph to a non-federated graph."); + let mut err = RoverError::new(err); + err.set_suggestion(Suggestion::ConvertGraphToSubgraph); + return Err(err); } let publish_response = publish::run( diff --git a/src/error/metadata/suggestion.rs b/src/error/metadata/suggestion.rs index 91400e24a..69e6bb4db 100644 --- a/src/error/metadata/suggestion.rs +++ b/src/error/metadata/suggestion.rs @@ -30,6 +30,7 @@ pub enum Suggestion { ProperKey, NewUserNoProfiles, CheckServerConnection, + ConvertGraphToSubgraph, } impl Display for Suggestion { @@ -124,7 +125,8 @@ impl Display for Suggestion { ) } Suggestion::Adhoc(msg) => msg.to_string(), - Suggestion::CheckServerConnection => "Make sure the endpoint accepting connections is spelled correctly".to_string() + Suggestion::CheckServerConnection => "Make sure the endpoint accepting connections is spelled correctly".to_string(), + Suggestion::ConvertGraphToSubgraph => "If you are sure you want to convert a non-federated graph to a subgraph, you can re-run the same command with a `--convert` flag.".to_string(), };