Skip to content

Commit

Permalink
Improved errors printing in case of terraform/tofu invocation (#3423)
Browse files Browse the repository at this point in the history
* Improved erorr printing to incude stderr and stdout

* lint fixes

* Test to do output of errors

* Updated handling of error message

* Updated log level
  • Loading branch information
denis256 authored Sep 20, 2024
1 parent 2a0eddc commit 70797fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions shell/run_shell_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func RunShellCommandWithOutput(
}

if err != nil {
opts.Logger.Warnf("Failed to execute %s in %s\n%s\n%s\n%v", command+" "+strings.Join(args, " "), cmd.Dir, stdoutBuf.String(), stderrBuf.String(), err)
err = util.ProcessExecutionError{
Err: err,
Stdout: stdoutBuf.String(),
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/error-print/custom-tf-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "Custom error from script" >&2

exit 1
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
testFixtureStack = "fixtures/stack/"
testFixtureStdout = "fixtures/download/stdout-test"
testFixtureTfTest = "fixtures/tftest/"
testFixtureErrorPrint = "fixtures/error-print"

terraformFolder = ".terraform"

Expand Down Expand Up @@ -3905,3 +3906,16 @@ func TestTerragruntJsonPlanJsonOutput(t *testing.T) {

}
}

func TestErrorMessageIncludeInOutput(t *testing.T) {
t.Parallel()

tmpEnvPath := copyEnvironment(t, testFixtureErrorPrint)
cleanupTerraformFolder(t, tmpEnvPath)
testPath := util.JoinPath(tmpEnvPath, testFixtureErrorPrint)

_, stderr, err := runTerragruntCommandWithOutput(t, "terragrunt apply --terragrunt-non-interactive --terragrunt-working-dir "+testPath+" --terragrunt-tfpath "+testPath+"/custom-tf-script.sh --terragrunt-log-level debug")
require.Error(t, err)

assert.Contains(t, stderr, "Custom error from script")
}

0 comments on commit 70797fd

Please sign in to comment.