From f8b72c8aaddd03e14f403b471c158847f8bdd160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 14 Apr 2023 22:30:37 +0200 Subject: [PATCH] prettify nested error messages. --- error.go | 4 +++- mapstructure.go | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/error.go b/error.go index 47a99e5a..344c52dc 100644 --- a/error.go +++ b/error.go @@ -16,12 +16,14 @@ type Error struct { func (e *Error) Error() string { points := make([]string, len(e.Errors)) for i, err := range e.Errors { + // split by newlines and indent each line + err = strings.Replace(err, "\n", "\n ", -1) points[i] = fmt.Sprintf("* %s", err) } sort.Strings(points) return fmt.Sprintf( - "%d error(s) decoding:\n\n%s", + "%d error(s) decoding:\n%s", len(e.Errors), strings.Join(points, "\n")) } diff --git a/mapstructure.go b/mapstructure.go index 7581806a..ce49da83 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -1441,7 +1441,12 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e } sort.Strings(keys) - err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", ")) + var err error + if name == "" { + err = fmt.Errorf("invalid keys: %s", strings.Join(keys, ", ")) + } else { + err = fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", ")) + } errors = appendErrors(errors, err) }