Skip to content

Commit

Permalink
fix: improved error msg (#936)
Browse files Browse the repository at this point in the history
This is how the logs are printed now for exec


<img width="1443" alt="Screen Shot 2023-07-18 at 1 58 49 PM"
src="https://github.com/kurtosis-tech/kurtosis/assets/15133250/624700b8-7bb1-47c6-9d2c-0f79396f75ea">
  • Loading branch information
Peeeekay committed Jul 18, 2023
1 parent dea74f2 commit 4f72ae1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Expand Up @@ -2,6 +2,7 @@ package exec

import (
"context"
"fmt"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_validator"
"github.com/kurtosis-tech/stacktrace"
"go.starlark.net/starlark"
"strings"
)

var defaultAcceptableCodes = []int64{
Expand Down Expand Up @@ -161,8 +163,10 @@ func (builtin *ExecCapabilities) Execute(ctx context.Context, _ *builtin_argumen
return "", stacktrace.Propagate(err, "Error executing exec recipe")
}
if !builtin.skipCodeCheck && !builtin.isAcceptableCode(result) {
return "", stacktrace.NewError("Exec returned exit code '%v' that is not part of the acceptable status codes '%v', with output:\n%v", result["code"], builtin.acceptableCodes, result["output"])
errorMessage := fmt.Sprintf("Exec returned exit code '%v' that is not part of the acceptable status codes '%v', with output:", result["code"], builtin.acceptableCodes)
return "", stacktrace.NewError(formatErrorMessage(errorMessage, result["output"].String()))
}

builtin.runtimeValueStore.SetValue(builtin.resultUuid, result)
instructionResult := builtin.execRecipe.ResultMapToString(result)
return instructionResult, err
Expand All @@ -178,3 +182,9 @@ func (builtin *ExecCapabilities) isAcceptableCode(recipeResult map[string]starla
}
return isAcceptableCode
}

func formatErrorMessage(errorMessage string, errorFromExec string) string {
splitErrorMessageNewLine := strings.Split(errorFromExec, "\n")
reformattedErrorMessage := strings.Join(splitErrorMessageNewLine, "\n ")
return fmt.Sprintf("%v\n %v", errorMessage, reformattedErrorMessage)
}
Expand Up @@ -312,9 +312,8 @@ func (builtin *RunPythonCapabilities) Execute(ctx context.Context, _ *builtin_ar

// throw an error as execution of the command failed
if runPythonExecutionResult.GetExitCode() != 0 {
return "", stacktrace.NewError(
"Python command: %q exited with code %d and output \n%v",
commandToRun, runPythonExecutionResult.GetExitCode(), runPythonExecutionResult.GetOutput())
errorMessage := fmt.Sprintf("Python command: %q exited with code %d and output", commandToRun, runPythonExecutionResult.GetExitCode())
return "", stacktrace.NewError(formatErrorMessage(errorMessage, runPythonExecutionResult.GetOutput()))
}

if builtin.fileArtifactNames != nil && builtin.pathToFileArtifacts != nil {
Expand Down
Expand Up @@ -218,9 +218,8 @@ func (builtin *RunShCapabilities) Execute(ctx context.Context, _ *builtin_argume

// throw an error as execution of the command failed
if createDefaultDirectoryResult.GetExitCode() != 0 {
return "", stacktrace.NewError(
"Shell command: %q exited with code %d and output \n%v",
commandToRun, createDefaultDirectoryResult.GetExitCode(), createDefaultDirectoryResult.GetOutput())
errorMessage := fmt.Sprintf("Shell command: %q exited with code %d and output", commandToRun, createDefaultDirectoryResult.GetExitCode())
return "", stacktrace.NewError(formatErrorMessage(errorMessage, createDefaultDirectoryResult.GetOutput()))
}

if builtin.fileArtifactNames != nil && builtin.pathToFileArtifacts != nil {
Expand Down
Expand Up @@ -254,3 +254,9 @@ func getServiceConfig(image string, filesArtifactExpansion *files_artifacts_expa
service_config.DefaultSubnetwork,
)
}

func formatErrorMessage(errorMessage string, errorFromExec string) string {
splitErrorMessageNewLine := strings.Split(errorFromExec, "\n")
reformattedErrorMessage := strings.Join(splitErrorMessageNewLine, "\n ")
return fmt.Sprintf("%v\n %v", errorMessage, reformattedErrorMessage)
}

0 comments on commit 4f72ae1

Please sign in to comment.