Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add nicer message for missing agent error #3996

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions cli/cmd/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func retryCommand(ctx context.Context) {
handleRootExecErr(rootCmd.ExecuteContext(ctx))
}

type errorMessageRenderer interface {
Render()
type errorMessager interface {
Message() string
}

const defaultErrorFormat = `
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions cli/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (c Configurator) createConfig(serverURL string) (Config, error) {
}

type invalidServerErr struct {
ui cliUI.UI
serverURL string
parent error
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions cli/runner/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Loading