From a91cc61bd616122c1922f3d5b5af70dfa948d435 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 30 Oct 2024 09:36:11 +0100 Subject: [PATCH] config: Pretty print yaml parser errors The `PrettyPrint()` method of the yaml errors doesn't get triggered automatically, so we've to do it via the `yaml.FormatError()` helper function. This will return the prettified string of the provided error, if it implements the `yaml.errors#PrettyPrinter` interface, otherwise just the error string. --- config/config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/config.go b/config/config.go index 4f025087..655d84c2 100644 --- a/config/config.go +++ b/config/config.go @@ -110,6 +110,10 @@ func FromYAMLFile(name string, v Validator) error { d := yaml.NewDecoder(f, yaml.DisallowUnknownField()) if err := d.Decode(v); err != nil { + // The PrettyPrint() method of the yaml parser errors doesn't get triggered automatically, so we've to do + // it via the `yaml.FormatError` helper function. If the provided error implements `yaml.errors.PrettyPrinter` + // we'll get the prettified string of that type, otherwise just error string. + err = errors.New(yaml.FormatError(err, true, true)) return errors.Wrap(err, "can't parse YAML file "+name) }