From f174289b34f8a023d5e9996487a06d9b7caa24a7 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 20 Sep 2023 13:54:59 -0700 Subject: [PATCH] Wrap errors when decoding map values This adds the key name, in particular, which can make it much easier to understand which field has an issue. --- decoder.go | 5 +++-- reader_test.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/decoder.go b/decoder.go index 8774531..435591e 100644 --- a/decoder.go +++ b/decoder.go @@ -2,6 +2,7 @@ package maxminddb import ( "encoding/binary" + "fmt" "math" "math/big" "reflect" @@ -615,7 +616,7 @@ func (d *decoder) decodeMap( offset, err = d.decode(offset, elemValue, depth) if err != nil { - return 0, err + return 0, fmt.Errorf("decoding value for %s: %w", key, err) } keyValue.SetString(string(key)) @@ -772,7 +773,7 @@ func (d *decoder) decodeStruct( offset, err = d.decode(offset, result.Field(j), depth) if err != nil { - return 0, err + return 0, fmt.Errorf("decoding value for %s: %w", key, err) } } return offset, nil diff --git a/reader_test.go b/reader_test.go index f40541e..53ff1cd 100644 --- a/reader_test.go +++ b/reader_test.go @@ -594,7 +594,7 @@ func TestBrokenDoubleDatabase(t *testing.T) { expected := newInvalidDatabaseError( "the MaxMind DB file's data section contains bad data (float 64 size of 2)", ) - assert.Equal(t, expected, err) + assert.ErrorAs(t, err, &expected) assert.NoError(t, reader.Close(), "error on close") }