Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettify nested error messages #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion error.go
Expand Up @@ -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"))
}

Expand Down
7 changes: 6 additions & 1 deletion mapstructure.go
Expand Up @@ -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)
}

Expand Down