better error handling when using interpolate funcs; don't swallow func errors
This commit is contained in:
parent
a87ce366b3
commit
2bbc3d50d7
|
@ -366,9 +366,15 @@ func (c *Core) init() error {
|
||||||
c.variables[k] = renderedV
|
c.variables[k] = renderedV
|
||||||
ctx.UserVariables = c.variables
|
ctx.UserVariables = c.variables
|
||||||
case ttmp.ExecError:
|
case ttmp.ExecError:
|
||||||
|
castError := err.(ttmp.ExecError)
|
||||||
|
switch castError.Err.(type) {
|
||||||
|
case interpolate.ErrVariableNotSet:
|
||||||
shouldRetry = true
|
shouldRetry = true
|
||||||
failedInterpolation = fmt.Sprintf(`"%s": "%s"`, k, v)
|
failedInterpolation = fmt.Sprintf(`"%s": "%s"; error: %s`, k, v, err)
|
||||||
continue
|
continue
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
// unexpected interpolation error: abort the run
|
// unexpected interpolation error: abort the run
|
||||||
|
|
|
@ -47,6 +47,14 @@ var FuncGens = map[string]FuncGenerator{
|
||||||
"lower": funcGenPrimitive(strings.ToLower),
|
"lower": funcGenPrimitive(strings.ToLower),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ErrVariableNotSet struct {
|
||||||
|
Var string // Name of template.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ErrVariableNotSet) Error() string {
|
||||||
|
return fmt.Sprintf("variable %s not set", e.Var)
|
||||||
|
}
|
||||||
|
|
||||||
// FuncGenerator is a function that given a context generates a template
|
// FuncGenerator is a function that given a context generates a template
|
||||||
// function for the template.
|
// function for the template.
|
||||||
type FuncGenerator func(*Context) interface{}
|
type FuncGenerator func(*Context) interface{}
|
||||||
|
@ -168,7 +176,7 @@ func funcGenUser(ctx *Context) interface{} {
|
||||||
// error and retry if we're interpolating UserVariables. But if
|
// error and retry if we're interpolating UserVariables. But if
|
||||||
// we're elsewhere in the template, just return the empty string.
|
// we're elsewhere in the template, just return the empty string.
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New(fmt.Sprintf("variable %s not set", k))
|
return "", ErrVariableNotSet{k}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return val, nil
|
return val, nil
|
||||||
|
|
Loading…
Reference in New Issue