Skip to content

Commit 2612fdd

Browse files
Merge pull request #37 from ipfs/feat/ser-errs
add special errors for denoting serialization issues
2 parents 85b7f1d + 9866602 commit 2612fdd

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ require (
1010
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb
1111
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
1212
)
13+
14+
go 1.13

hamt_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,45 @@ func TestBasic(t *testing.T) {
273273
}
274274
}
275275

276+
func TestDelete(t *testing.T) {
277+
ctx := context.Background()
278+
cs := NewCborStore()
279+
begn := NewNode(cs)
280+
281+
val := []byte("cat dog bear")
282+
if err := begn.Set(ctx, "foo", val); err != nil {
283+
t.Fatal(err)
284+
}
285+
286+
for i := 0; i < 10; i++ {
287+
if err := begn.Set(ctx, randString(), randValue()); err != nil {
288+
t.Fatal(err)
289+
}
290+
}
291+
292+
if err := begn.Flush(ctx); err != nil {
293+
t.Fatal(err)
294+
}
295+
c, err := cs.Put(ctx, begn)
296+
if err != nil {
297+
t.Fatal(err)
298+
}
299+
300+
n, err := LoadNode(ctx, cs, c)
301+
if err != nil {
302+
t.Fatal(err)
303+
}
304+
305+
if err := n.Delete(ctx, "foo"); err != nil {
306+
t.Fatal(err)
307+
}
308+
309+
var out []byte
310+
if err := n.Find(ctx, "foo", &out); err == nil {
311+
t.Fatal("shouldnt have found object")
312+
}
313+
}
314+
276315
func TestSetGet(t *testing.T) {
277316
ctx := context.Background()
278317
vals := make(map[string][]byte)

ipld.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ type CborIpldStore struct {
5050
Atlas *atlas.Atlas
5151
}
5252

53+
func NewSerializationError(err error) error {
54+
return SerializationError{err}
55+
}
56+
57+
type SerializationError struct {
58+
err error
59+
}
60+
61+
func (se SerializationError) Error() string {
62+
return se.err.Error()
63+
}
64+
65+
func (se SerializationError) Unwrap() error {
66+
return se.err
67+
}
68+
5369
type blocks interface {
5470
GetBlock(context.Context, cid.Cid) (block.Block, error)
5571
AddBlock(block.Block) error
@@ -114,7 +130,11 @@ func (s *CborIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) err
114130

115131
cu, ok := out.(cbg.CBORUnmarshaler)
116132
if ok {
117-
return cu.UnmarshalCBOR(bytes.NewReader(blk.RawData()))
133+
if err := cu.UnmarshalCBOR(bytes.NewReader(blk.RawData())); err != nil {
134+
return NewSerializationError(err)
135+
}
136+
137+
return nil
118138
}
119139

120140
if s.Atlas == nil {
@@ -146,7 +166,7 @@ func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
146166
if ok {
147167
buf := new(bytes.Buffer)
148168
if err := cm.MarshalCBOR(buf); err != nil {
149-
return cid.Undef, err
169+
return cid.Undef, NewSerializationError(err)
150170
}
151171

152172
pref := cid.Prefix{

0 commit comments

Comments
 (0)