From 6ee25f1043b50217f50f1e7df0d2c0e96f024402 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Thu, 9 May 2024 15:15:13 +0900 Subject: [PATCH] fix: exit commands with non zero exit code if plan or apply fails (#1257) * fix: exit commands with non zero exit code if plan or apply fails * refactor: refactor * fix: return non zero if error is nil --- pkg/apperr/error.go | 11 +++++++++-- pkg/apperr/error_test.go | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/apperr/error.go b/pkg/apperr/error.go index 2c3be878..0f15dcf4 100644 --- a/pkg/apperr/error.go +++ b/pkg/apperr/error.go @@ -62,14 +62,21 @@ func HandleExit(err error) int { logE := logrus.NewEntry(logrus.New()) if exitErr, ok := err.(ExitCoder); ok { //nolint:errorlint - if err.Error() != "" { + errMsg := err.Error() + if errMsg != "" { if _, ok := exitErr.(ErrorFormatter); ok { logrus.Errorf("%+v", err) } else { logerr.WithError(logE, err).Error("tfcmt failed") } } - return exitErr.ExitCode() + if code := exitErr.ExitCode(); code != 0 { + return code + } + if errMsg == "" { + return ExitCodeOK + } + return ExitCodeError } logerr.WithError(logE, err).Error("tfcmt failed") diff --git a/pkg/apperr/error_test.go b/pkg/apperr/error_test.go index b3a7467e..24c53bac 100644 --- a/pkg/apperr/error_test.go +++ b/pkg/apperr/error_test.go @@ -20,7 +20,7 @@ func TestHandleError(t *testing.T) { { name: "case 1", err: NewExitError(0, errors.New("error")), - exitCode: 0, + exitCode: 1, }, { name: "case 2",