From b9e3b9c370cccd82380201c264288cd293856f9d Mon Sep 17 00:00:00 2001 From: Johanan Idicula Date: Sun, 30 Oct 2022 10:44:16 -0700 Subject: [PATCH] refactor: Use typeMismatchError rather than raw string error (#826) Uses the existing method to DRY up the error message generation, and decorates with position index where needed. No behaviour is changed, but it allows for further changes to make error messaging more specific. Related to: pelletier/go-toml#806 --- unmarshaler.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index d0d7a72..32206ff 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -339,13 +339,13 @@ func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Val case reflect.Array: idx := d.arrayIndex(true, v) if idx >= v.Len() { - return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx) + return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx) } elem := v.Index(idx) _, err := d.handleArrayTable(key, elem) return v, err default: - return reflect.Value{}, fmt.Errorf("toml: cannot decode array table into a %s", v.Type()) + return reflect.Value{}, d.typeMismatchError("array table", v.Type()) } } @@ -390,7 +390,7 @@ func (d *decoder) handleArrayTableCollection(key ast.Iterator, v reflect.Value) case reflect.Array: idx := d.arrayIndex(false, v) if idx >= v.Len() { - return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx) + return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx) } elem := v.Index(idx) _, err := d.handleArrayTable(key, elem)