diff --git a/interpolation/interpolation_test.go b/interpolation/interpolation_test.go index b67eee40..85027311 100644 --- a/interpolation/interpolation_test.go +++ b/interpolation/interpolation_test.go @@ -114,6 +114,7 @@ func TestValidUnexistentInterpolation(t *testing.T) { {test: "{{{ ${FOO:?foo_} }}}", errMsg: "foo_"}, {test: "{{{ ${FOO:?foo-bar-value} }}}", errMsg: "foo-bar-value"}, {test: "{{{ ${FOO:?foo} ${BAR:-DEFAULT_VALUE} }}}", errMsg: "foo"}, + {test: "${FOO:?foo} ${BAR:?bar}", errMsg: "foo"}, {test: "{{{ ${BAR} }}}", expected: "{{{ }}}"}, {test: "${FOO:?baz} }}}", errMsg: "baz"}, {test: "${FOO?baz} }}}", errMsg: "baz"}, diff --git a/template/template.go b/template/template.go index 1e497ab3..7056a13a 100644 --- a/template/template.go +++ b/template/template.go @@ -62,6 +62,7 @@ type SubstituteFunc func(string, Mapping) (string, bool, error) // It accepts additional substitute function. func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { var outerErr error + var returnErr error result := pattern.ReplaceAllStringFunc(template, func(substring string) string { _, subsFunc := getSubstitutionFunctionForTemplate(substring) @@ -91,6 +92,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su if substitution == "" { outerErr = &InvalidTemplateError{Template: template} + if returnErr == nil { + returnErr = outerErr + } return "" } @@ -101,6 +105,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su ) value, applied, outerErr = subsFunc(substitution, mapping) if outerErr != nil { + if returnErr == nil { + returnErr = outerErr + } return "" } if applied { @@ -119,7 +126,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su return value }) - return result, outerErr + return result, returnErr } func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {