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: correctly display general errors #3995

Merged
merged 3 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
31 changes: 2 additions & 29 deletions cli/cloud/runner/multifile_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"

Expand All @@ -15,6 +13,7 @@ import (
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/kubeshop/tracetest/cli/runner"

"github.com/kubeshop/tracetest/cli/varset"
"github.com/kubeshop/tracetest/server/pkg/id"
"go.uber.org/zap"
Expand Down Expand Up @@ -309,7 +308,7 @@ func (o orchestrator) createRun(ctx context.Context, resource any, vars *varset.
}
if !errors.Is(err, varset.MissingVarsError{}) {
// actual error, return
return result, resource, fmt.Errorf("cannot run test: %w", err)
return result, resource, fmt.Errorf("cannot create test run: %w", err)
}

// missing vars error
Expand Down Expand Up @@ -357,29 +356,3 @@ func getRequiredGates(gates []string) []openapi.SupportedGates {

return requiredGates
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was unused

// 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
// so the orchestrator can request them from the user.
func HandleRunError(resp *http.Response, reqErr error) error {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read response body: %w", err)
}
resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("resource not found in server")
}

if resp.StatusCode == http.StatusUnprocessableEntity {
return varset.BuildMissingVarsError(body)
}

if reqErr != nil {
return fmt.Errorf("could not run test suite: %w", reqErr)
}

return nil
}
8 changes: 7 additions & 1 deletion cli/cmd/resource_run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"

cloudCmd "github.com/kubeshop/tracetest/cli/cloud/cmd"
cliRunner "github.com/kubeshop/tracetest/cli/runner"
)

var (
Expand Down Expand Up @@ -109,7 +110,12 @@ func runMultipleFiles(ctx context.Context) (string, error) {
}

exitCode, err := cloudCmd.RunMultipleFiles(ctx, cliLogger, httpClient, runParams, &cliConfig, runnerRegistry, output)
ExitCLI(exitCode)
// General Error is 1, which is the default for errors. if this is the case,
// let the CLI handle the error and exit.
// otherwise exit with the exit code.
if exitCode > cliRunner.ExitCodeGeneralError {
ExitCLI(exitCode)
}
return "", err
}

Expand Down
24 changes: 21 additions & 3 deletions cli/runner/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ func (a orchestrator) writeJUnitReport(ctx context.Context, r Runner, result Run
return err
}

var source = "cli"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this var was unused


func getRequiredGates(gates []string) []openapi.SupportedGates {
if len(gates) == 0 {
return nil
Expand Down Expand Up @@ -330,9 +328,29 @@ func HandleRunError(resp *http.Response, reqErr error) error {
return varset.BuildMissingVarsError(body)
}

if ok, msg := attemptToParseStructuredError(body); ok {
return fmt.Errorf("could not run resouce: %s", msg)
}

if reqErr != nil {
return fmt.Errorf("could not run test suite: %w", reqErr)
return fmt.Errorf("could not run resouce: %w", reqErr)
}

return nil
}

func attemptToParseStructuredError(body []byte) (bool, string) {
var parsed struct {
Status int `json:"status"`
Detail string `json:"detail"`
}

err := jsonFormat.Unmarshal(body, &parsed)
if err != nil || parsed.Status == 0 {
return false, ""
}

msg := fmt.Sprintf("%s (code %d)", parsed.Detail, parsed.Status)

return true, msg
}
Loading