Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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