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 Sep 20, 2023
1 parent e970a48 commit f174289
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)
assert.ErrorAs(t, err, &expected)
assert.NoError(t, reader.Close(), "error on close")
}

Expand Down

0 comments on commit f174289

Please sign in to comment.