Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: clean the error for starlark output as well (#413)
## Description: This PR will keep the errors consistent for Starlark execution phase. It will not show the stack-trace path unless users either pass in `cli-log-level debug` or they can see the logs with stack-traces in `kurtosis-cli.log` file. This PR just does the same for starlark as #369 did for cli. ## Is this change user facing? YES/NO <!-- If yes, please add the "user facing" label to the PR --> <!-- If yes, don't forget to include docs changes where relevant --> ## References (if applicable): <!-- Add relevant Github Issues, Discord threads, or other helpful information. -->
- Loading branch information
Showing
6 changed files
with
87 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package out | ||
|
||
import ( | ||
"errors" | ||
"github.com/sirupsen/logrus" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ( | ||
lineWithStacktraceRegex = "^--- at\\s*(.*?):([\\d]*)\\s*\\((.*?)\\)\\s*---$" | ||
separator = "\n" | ||
errorNotCreatedFromStacktrace = 1 | ||
) | ||
|
||
var lineWithStacktrace = regexp.MustCompile(lineWithStacktraceRegex) | ||
|
||
func GetErrorMessageToBeDisplayedOnCli(errorWithStacktrace error) error { | ||
// if we are running in the debug mode, just return the error with stack-traces back to the client | ||
if logrus.GetLevel() == logrus.DebugLevel { | ||
return errorWithStacktrace | ||
} | ||
|
||
// silently catch the file logger error and print it in the debug mode | ||
// users should not worry about this error | ||
// downside is that we may lose stack-traces during file logger failures | ||
fileLogger, err := GetFileLogger() | ||
if err != nil { | ||
logrus.Warnf("Error occurred while getting the file logger %+v", err) | ||
} else { | ||
fileLogger.Errorln(errorWithStacktrace.Error()) | ||
} | ||
|
||
errorMessage := errorWithStacktrace.Error() | ||
cleanError := removeFilePathFromErrorMessage(errorMessage) | ||
return cleanError | ||
} | ||
|
||
// this method removes the file path from the error | ||
func removeFilePathFromErrorMessage(errorMessage string) error { | ||
errorMessageConvertedInList := strings.Split(errorMessage, separator) | ||
// safe to assume that the error was not generated using stacktrace package | ||
if len(errorMessageConvertedInList) == errorNotCreatedFromStacktrace { | ||
return errors.New(errorMessage) | ||
} | ||
|
||
// only the even numbered elements needs to be picked. | ||
var cleanErrorList []string | ||
for _, line := range errorMessageConvertedInList { | ||
// this only cleans spaces for the lines that contains the stack-trace information | ||
cleanLine := strings.TrimSpace(line) | ||
if !lineWithStacktrace.MatchString(cleanLine) { | ||
cleanErrorList = append(cleanErrorList, cleanLine) | ||
} | ||
} | ||
|
||
cleanErrorMessage := strings.Join(cleanErrorList, "\n") | ||
return errors.New(cleanErrorMessage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters