Merge pull request #70 from docker/compose-template-substitute-error-signature

Return an error instead of an `*InvalidTemplateError`
This commit is contained in:
Sebastiaan van Stijn 2017-05-12 16:51:37 +02:00 committed by GitHub
commit bb1a403080
2 changed files with 9 additions and 4 deletions

View File

@ -39,11 +39,15 @@ func interpolateSectionItem(
for key, value := range item { for key, value := range item {
interpolatedValue, err := recursiveInterpolate(value, mapping) interpolatedValue, err := recursiveInterpolate(value, mapping)
if err != nil { switch err := err.(type) {
case nil:
case *template.InvalidTemplateError:
return nil, errors.Errorf( return nil, errors.Errorf(
"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.", "Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
key, section, name, err.Template, key, section, name, err.Template,
) )
default:
return nil, errors.Wrapf(err, "error while interpolating %s in %s %s", key, section, name)
} }
out[key] = interpolatedValue out[key] = interpolatedValue
} }
@ -55,7 +59,7 @@ func interpolateSectionItem(
func recursiveInterpolate( func recursiveInterpolate(
value interface{}, value interface{},
mapping template.Mapping, mapping template.Mapping,
) (interface{}, *template.InvalidTemplateError) { ) (interface{}, error) {
switch value := value.(type) { switch value := value.(type) {

View File

@ -33,8 +33,9 @@ func (e InvalidTemplateError) Error() string {
type Mapping func(string) (string, bool) type Mapping func(string) (string, bool)
// Substitute variables in the string with their values // Substitute variables in the string with their values
func Substitute(template string, mapping Mapping) (result string, err *InvalidTemplateError) { func Substitute(template string, mapping Mapping) (string, error) {
result = pattern.ReplaceAllStringFunc(template, func(substring string) string { var err error
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
matches := pattern.FindStringSubmatch(substring) matches := pattern.FindStringSubmatch(substring)
groups := make(map[string]string) groups := make(map[string]string)
for i, name := range pattern.SubexpNames() { for i, name := range pattern.SubexpNames() {