Skip to content

Commit

Permalink
Wrap errors when decoding map values
Browse files Browse the repository at this point in the history
This adds the key name, in particular, which can make it much easier
to understand which field has an issue.
  • Loading branch information
oschwald committed Jun 2, 2024
1 parent 60242ba commit 32f205a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package maxminddb

import (
"encoding/binary"
"fmt"
"math"
"math/big"
"reflect"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
require.ErrorAs(t, err, &expected)
require.NoError(t, reader.Close(), "error on close")
}

Expand Down

0 comments on commit 32f205a

Please sign in to comment.