From b190a1a0e74e6b98c65988b525e0f2b1a47bfec8 Mon Sep 17 00:00:00 2001 From: jidicula Date: Wed, 26 Oct 2022 20:51:35 -0400 Subject: [PATCH] refactor: Use typeMismatchError rather than raw string error 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 d0d7a72d..32206ff2 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)