diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-01 02:41:14 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-01 02:41:14 -0600 |
commit | d6243eb7afd5d9d91686bce2e02c252be456b38c (patch) | |
tree | 11d5df97a41b211aa69777a020e00d47a8790329 /pkg | |
parent | 4d3a645624cd6aafcd081edf8ef833ace27dfb87 (diff) |
better errors
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/binstruct/size.go | 3 | ||||
-rw-r--r-- | pkg/binstruct/structs.go | 35 | ||||
-rw-r--r-- | pkg/btrfs/btrfsitem/item_chunk.go | 4 |
3 files changed, 23 insertions, 19 deletions
diff --git a/pkg/binstruct/size.go b/pkg/binstruct/size.go index 0119a7a..6563455 100644 --- a/pkg/binstruct/size.go +++ b/pkg/binstruct/size.go @@ -48,7 +48,8 @@ func staticSize(typ reflect.Type) (int, error) { if !(typ.Implements(marshalerType) || typ.Implements(unmarshalerType)) { return getStructHandler(typ).Size, nil } - fallthrough + return 0, fmt.Errorf("type=%v (kind=%v) does not implement binfmt.StaticSizer but does implement binfmt.Marshaler or binfmt.Unmarshaler", + typ, typ.Kind()) default: return 0, fmt.Errorf("type=%v does not implement binfmt.StaticSizer and kind=%v is not a supported statically-sized kind", typ, typ.Kind()) diff --git a/pkg/binstruct/structs.go b/pkg/binstruct/structs.go index 6e9f02f..7a0c74b 100644 --- a/pkg/binstruct/structs.go +++ b/pkg/binstruct/structs.go @@ -55,6 +55,7 @@ func parseStructTag(str string) (tag, error) { } type structHandler struct { + name string Size int fields []structField } @@ -75,12 +76,12 @@ func (sh structHandler) Unmarshal(dat []byte, dst reflect.Value) (int, error) { if _n >= 0 { n += _n } - return n, fmt.Errorf("field %d %q: %w", - i, field.name, err) + return n, fmt.Errorf("struct %q field %d %q: %w", + sh.name, i, field.name, err) } if _n != field.siz { - return n, fmt.Errorf("field %d %q: consumed %d bytes but should have consumed %d bytes", - i, field.name, _n, field.siz) + return n, fmt.Errorf("struct %q field %d %q: consumed %d bytes but should have consumed %d bytes", + sh.name, i, field.name, _n, field.siz) } n += _n } @@ -96,8 +97,8 @@ func (sh structHandler) Marshal(val reflect.Value) ([]byte, error) { bs, err := Marshal(val.Field(i).Interface()) ret = append(ret, bs...) if err != nil { - return ret, fmt.Errorf("field %d %q: %w", - i, field.name, err) + return ret, fmt.Errorf("struct %q field %d %q: %w", + sh.name, i, field.name, err) } } return ret, nil @@ -106,14 +107,16 @@ func (sh structHandler) Marshal(val reflect.Value) ([]byte, error) { func genStructHandler(structInfo reflect.Type) (structHandler, error) { var ret structHandler + ret.name = structInfo.String() + var curOffset, endOffset int for i := 0; i < structInfo.NumField(); i++ { var fieldInfo reflect.StructField = structInfo.Field(i) fieldTag, err := parseStructTag(fieldInfo.Tag.Get("bin")) if err != nil { - return ret, fmt.Errorf("%v: field %q: %w", - structInfo, fieldInfo.Name, err) + return ret, fmt.Errorf("struct %q field %d %q: %w", + ret.name, i, fieldInfo.Name, err) } if fieldTag.skip { ret.fields = append(ret.fields, structField{ @@ -125,8 +128,8 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { if fieldTag.off != curOffset { err := fmt.Errorf("tag says off=0x%x but curOffset=0x%x", fieldTag.off, curOffset) - return ret, fmt.Errorf("%v: field %q: %w", - structInfo, fieldInfo.Name, err) + return ret, fmt.Errorf("struct %q field %d %q: %w", + ret.name, i, fieldInfo.Name, err) } if fieldInfo.Type == endType { endOffset = curOffset @@ -134,14 +137,14 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { fieldSize, err := staticSize(fieldInfo.Type) if err != nil { - return ret, fmt.Errorf("%v: field %q: %w", - structInfo, fieldInfo.Name, err) + return ret, fmt.Errorf("struct %q field %d %q: %w", + ret.name, i, fieldInfo.Name, err) } if fieldTag.siz != fieldSize { err := fmt.Errorf("tag says siz=0x%x but StaticSize(typ)=0x%x", fieldTag.siz, fieldSize) - return ret, fmt.Errorf("%v: field %q: %w", - structInfo, fieldInfo.Name, err) + return ret, fmt.Errorf("struct %q field %d %q: %w", + ret.name, i, fieldInfo.Name, err) } curOffset += fieldTag.siz @@ -153,8 +156,8 @@ func genStructHandler(structInfo reflect.Type) (structHandler, error) { ret.Size = curOffset if ret.Size != endOffset { - return ret, fmt.Errorf("%v: .Size=%v but endOffset=%v", - structInfo, ret.Size, endOffset) + return ret, fmt.Errorf("struct %q: .Size=%v but endOffset=%v", + ret.name, ret.Size, endOffset) } return ret, nil diff --git a/pkg/btrfs/btrfsitem/item_chunk.go b/pkg/btrfs/btrfsitem/item_chunk.go index 17f069c..5c573b0 100644 --- a/pkg/btrfs/btrfsitem/item_chunk.go +++ b/pkg/btrfs/btrfsitem/item_chunk.go @@ -38,7 +38,7 @@ func (chunk *Chunk) UnmarshalBinary(dat []byte) (int, error) { _n, err := binstruct.Unmarshal(dat[n:], &stripe) n += _n if err != nil { - return n, err + return n, fmt.Errorf("%T.UnmarshalBinary: %w", *chunk, err) } chunk.Stripes = append(chunk.Stripes, stripe) } @@ -55,7 +55,7 @@ func (chunk Chunk) MarshalBinary() ([]byte, error) { _ret, err := binstruct.Marshal(stripe) ret = append(ret, _ret...) if err != nil { - return ret, err + return ret, fmt.Errorf("%T.MarshalBinary: %w", chunk, err) } } return ret, nil |