Merge pull request #7854 from hashicorp/validate_vault_env
better error handling when using interpolate funcs; don't swallow fun…
This commit is contained in:
commit
5478f16e19
|
@ -3,6 +3,7 @@ package packer
|
|||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
ttmp "text/template"
|
||||
|
||||
|
@ -366,9 +367,13 @@ func (c *Core) init() error {
|
|||
c.variables[k] = renderedV
|
||||
ctx.UserVariables = c.variables
|
||||
case ttmp.ExecError:
|
||||
shouldRetry = true
|
||||
failedInterpolation = fmt.Sprintf(`"%s": "%s"`, k, v)
|
||||
continue
|
||||
castError := err.(ttmp.ExecError)
|
||||
if strings.Contains(castError.Error(), interpolate.ErrVariableNotSetString) {
|
||||
shouldRetry = true
|
||||
failedInterpolation = fmt.Sprintf(`"%s": "%s"; error: %s`, k, v, err)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
// unexpected interpolation error: abort the run
|
||||
|
|
|
@ -47,6 +47,8 @@ var FuncGens = map[string]FuncGenerator{
|
|||
"lower": funcGenPrimitive(strings.ToLower),
|
||||
}
|
||||
|
||||
var ErrVariableNotSetString = "Error: variable not set:"
|
||||
|
||||
// FuncGenerator is a function that given a context generates a template
|
||||
// function for the template.
|
||||
type FuncGenerator func(*Context) interface{}
|
||||
|
@ -168,7 +170,7 @@ func funcGenUser(ctx *Context) interface{} {
|
|||
// error and retry if we're interpolating UserVariables. But if
|
||||
// we're elsewhere in the template, just return the empty string.
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("variable %s not set", k))
|
||||
return "", fmt.Errorf("%s %s", ErrVariableNotSetString, k)
|
||||
}
|
||||
}
|
||||
return val, nil
|
||||
|
|
Loading…
Reference in New Issue