tfdiags: FormatErrorPrefixed

When presenting an error that may be a PathError, the error's path is
usually relative to some other value. If the caller is able to express
that value (or, more often, a reference to it) in HCL syntax then this
method will produce a complete expression in the error message,
concatenating any path information from the error to the end of the given
prefix string.
This commit is contained in:
Martin Atkins 2018-08-28 17:11:36 -07:00
parent 182e783885
commit d96284f2e2
1 changed files with 12 additions and 0 deletions

View File

@ -54,3 +54,15 @@ func FormatError(err error) string {
return fmt.Sprintf("%s: %s", FormatCtyPath(perr.Path), perr.Error())
}
// FormatErrorPrefixed is like FormatError except that it presents any path
// information after the given prefix string, which is assumed to contain
// an HCL syntax representation of the value that errors are relative to.
func FormatErrorPrefixed(err error, prefix string) string {
perr, ok := err.(cty.PathError)
if !ok || len(perr.Path) == 0 {
return fmt.Sprintf("%s: %s", prefix, err.Error())
}
return fmt.Sprintf("%s%s: %s", prefix, FormatCtyPath(perr.Path), perr.Error())
}