From a4e26e25778156c21f2da160a11e6bd5425bf6a6 Mon Sep 17 00:00:00 2001 From: Denis O Date: Mon, 11 Nov 2024 18:56:21 +0200 Subject: [PATCH] Improved errors handling for HCL functions (#3544) * Improved multi error handling * multi error handling * Lint error fixes --- config/config_helpers.go | 2 +- internal/errors/multierror.go | 12 ++++++++++++ test/integration_sops_test.go | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config/config_helpers.go b/config/config_helpers.go index 1d9505780..08f0bafc6 100644 --- a/config/config_helpers.go +++ b/config/config_helpers.go @@ -1114,7 +1114,7 @@ func extractSopsErrors(err error) *errors.MultiError { } // append the original error if no group results were found - if errs == nil { + if errs.Len() == 0 { errs = errs.Append(err) } diff --git a/internal/errors/multierror.go b/internal/errors/multierror.go index b4adcedb9..95ae8dabe 100644 --- a/internal/errors/multierror.go +++ b/internal/errors/multierror.go @@ -46,11 +46,23 @@ func (errs *MultiError) Append(appendErrs ...error) *MultiError { errs = &MultiError{inner: new(multierror.Error)} } + if errs.inner == nil { + errs.inner = new(multierror.Error) + } + return &MultiError{inner: multierror.Append(errs.inner, appendErrs...)} } // Len implements sort.Interface function for length. func (errs *MultiError) Len() int { + if errs == nil { + errs = &MultiError{inner: new(multierror.Error)} + } + + if errs.inner == nil { + errs.inner = new(multierror.Error) + } + return len(errs.inner.Errors) } diff --git a/test/integration_sops_test.go b/test/integration_sops_test.go index a19cc91f8..9b5ac06ce 100644 --- a/test/integration_sops_test.go +++ b/test/integration_sops_test.go @@ -111,4 +111,6 @@ func TestSopsDecryptOnMissing(t *testing.T) { require.Error(t, err) assert.Contains(t, errorOut, "Encountered error while evaluating locals in file ./terragrunt.hcl") + assert.Contains(t, errorOut, "./missing.yaml: no such file") + }