From 7f68e40bd5c0825d5886dd8d5cde74e00d738444 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Wed, 28 Aug 2024 18:57:04 -0300 Subject: [PATCH 1/4] fix: correctly display general errors --- cli/runner/orchestrator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/runner/orchestrator.go b/cli/runner/orchestrator.go index 3d75dd4004..b0e3ffbd1c 100644 --- a/cli/runner/orchestrator.go +++ b/cli/runner/orchestrator.go @@ -346,7 +346,7 @@ func attemptToParseStructuredError(body []byte) (bool, string) { } err := jsonFormat.Unmarshal(body, &parsed) - if err != nil || parsed.Status == 0 { + if err != nil { return false, "" } From 16fcfe7bc2ceb2c4ec5ba59ec9405c58178713d5 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Wed, 28 Aug 2024 19:04:37 -0300 Subject: [PATCH 2/4] fix --- cli/runner/orchestrator.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/runner/orchestrator.go b/cli/runner/orchestrator.go index b0e3ffbd1c..5588329a4d 100644 --- a/cli/runner/orchestrator.go +++ b/cli/runner/orchestrator.go @@ -329,6 +329,7 @@ func HandleRunError(resp *http.Response, reqErr error) error { } if ok, msg := attemptToParseStructuredError(body); ok { + spew.Dump(body) return fmt.Errorf("could not run resouce: %s", msg) } @@ -346,7 +347,7 @@ func attemptToParseStructuredError(body []byte) (bool, string) { } err := jsonFormat.Unmarshal(body, &parsed) - if err != nil { + if err != nil || parsed.Status == 0 { return false, "" } From afcc37a430fc2348bd320a180e9b8731c64fd448 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Wed, 28 Aug 2024 19:20:57 -0300 Subject: [PATCH 3/4] fixes --- cli/runner/orchestrator.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/runner/orchestrator.go b/cli/runner/orchestrator.go index 5588329a4d..3d75dd4004 100644 --- a/cli/runner/orchestrator.go +++ b/cli/runner/orchestrator.go @@ -329,7 +329,6 @@ func HandleRunError(resp *http.Response, reqErr error) error { } if ok, msg := attemptToParseStructuredError(body); ok { - spew.Dump(body) return fmt.Errorf("could not run resouce: %s", msg) } From 43f563e517582a476360d2023ccd2a3b8d7d8e2c Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Wed, 28 Aug 2024 19:37:18 -0300 Subject: [PATCH 4/4] fix: add nicer message for missing agent error --- cli/cmd/middleware.go | 21 +++++++++++++++------ cli/config/configurator.go | 8 +++----- cli/runner/orchestrator.go | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cli/cmd/middleware.go b/cli/cmd/middleware.go index deebc37b32..444add4fdb 100644 --- a/cli/cmd/middleware.go +++ b/cli/cmd/middleware.go @@ -82,8 +82,8 @@ func retryCommand(ctx context.Context) { handleRootExecErr(rootCmd.ExecuteContext(ctx)) } -type errorMessageRenderer interface { - Render() +type errorMessager interface { + Message() string } const defaultErrorFormat = ` @@ -96,16 +96,25 @@ An error ocurred when executing the command ` func OnError(err error) { - errorMessage := handleErrorMessage(err) - - if renderer, ok := err.(errorMessageRenderer); ok { - renderer.Render() + if renderer := findErrorMessageRenderer(err); renderer != nil { + ui.DefaultUI.Error(renderer.Message()) } else { + errorMessage := handleErrorMessage(err) fmt.Fprintf(os.Stderr, defaultErrorFormat, versionText, errorMessage) } ExitCLI(1) } +func findErrorMessageRenderer(err error) errorMessager { + for err != nil { + if renderer, ok := err.(errorMessager); ok { + return renderer + } + err = errors.Unwrap(err) + } + return nil +} + func handleErrorMessage(err error) string { var requestError resourcemanager.RequestError hasRequestError := errors.As(err, &requestError) diff --git a/cli/config/configurator.go b/cli/config/configurator.go index 999e4286cc..244b5fe479 100644 --- a/cli/config/configurator.go +++ b/cli/config/configurator.go @@ -176,7 +176,6 @@ func (c Configurator) createConfig(serverURL string) (Config, error) { } type invalidServerErr struct { - ui cliUI.UI serverURL string parent error } @@ -185,9 +184,8 @@ func (e invalidServerErr) Error() string { return fmt.Errorf("cannot reach %s: %w", e.serverURL, e.parent).Error() } -func (e invalidServerErr) Render() { - msg := fmt.Sprintf(`Cannot reach "%s". Please verify the url and enter it again.`, e.serverURL) - e.ui.Error(msg) +func (e invalidServerErr) Message() string { + return fmt.Sprintf(`Cannot reach "%s". Please verify the url and enter it again.`, e.serverURL) } func (c Configurator) populateConfigWithDevConfig(_ context.Context, cfg *Config) { @@ -227,7 +225,7 @@ func (c Configurator) populateConfigWithVersionInfo(ctx context.Context, cfg Con client := GetAPIClient(cfg) version, err := getVersionMetadata(ctx, client) if err != nil { - err = invalidServerErr{c.ui, c.finalServerURL, fmt.Errorf("cannot get version metadata: %w", err)} + err = invalidServerErr{c.finalServerURL, fmt.Errorf("cannot get version metadata: %w", err)} return Config{}, err, false } diff --git a/cli/runner/orchestrator.go b/cli/runner/orchestrator.go index 3d75dd4004..327914ac5a 100644 --- a/cli/runner/orchestrator.go +++ b/cli/runner/orchestrator.go @@ -309,6 +309,20 @@ func getRequiredGates(gates []string) []openapi.SupportedGates { return requiredGates } +var ( + ErrAgentNotStarted = agentNotStartedError{} +) + +type agentNotStartedError struct{} + +func (e agentNotStartedError) Error() string { + return "agent not started" +} + +func (e agentNotStartedError) Message() string { + return "Agent is not started for this environment. Please start the agent and try again." +} + // HandleRunError handles errors returned by the server when running a test. // It normalizes the handling of general errors, like 404, // but more importantly, it processes the missing environment variables error @@ -328,6 +342,10 @@ func HandleRunError(resp *http.Response, reqErr error) error { return varset.BuildMissingVarsError(body) } + if resp.StatusCode == http.StatusFailedDependency { + return ErrAgentNotStarted + } + if ok, msg := attemptToParseStructuredError(body); ok { return fmt.Errorf("could not run resouce: %s", msg) }