summaryrefslogtreecommitdiff
path: root/lib/binstruct/marshal.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/binstruct/marshal.go')
-rw-r--r--lib/binstruct/marshal.go27
1 files changed, 23 insertions, 4 deletions
diff --git a/lib/binstruct/marshal.go b/lib/binstruct/marshal.go
index 8159191..78a4bb5 100644
--- a/lib/binstruct/marshal.go
+++ b/lib/binstruct/marshal.go
@@ -14,7 +14,15 @@ type Marshaler = encoding.BinaryMarshaler
func Marshal(obj any) ([]byte, error) {
if mar, ok := obj.(Marshaler); ok {
- return mar.MarshalBinary()
+ dat, err := mar.MarshalBinary()
+ if err != nil {
+ err = &UnmarshalError{
+ Type: reflect.TypeOf(obj),
+ Method: "MarshalBinary",
+ Err: err,
+ }
+ }
+ return dat, err
}
return MarshalWithoutInterface(obj)
}
@@ -24,7 +32,15 @@ func MarshalWithoutInterface(obj any) ([]byte, error) {
switch val.Kind() {
case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64:
typ := intKind2Type[val.Kind()]
- return val.Convert(typ).Interface().(Marshaler).MarshalBinary()
+ dat, err := val.Convert(typ).Interface().(Marshaler).MarshalBinary()
+ if err != nil {
+ err = &UnmarshalError{
+ Type: typ,
+ Method: "MarshalBinary",
+ Err: err,
+ }
+ }
+ return dat, err
case reflect.Ptr:
return Marshal(val.Elem().Interface())
case reflect.Array:
@@ -40,7 +56,10 @@ func MarshalWithoutInterface(obj any) ([]byte, error) {
case reflect.Struct:
return getStructHandler(val.Type()).Marshal(val)
default:
- panic(fmt.Errorf("type=%v does not implement binfmt.Marshaler and kind=%v is not a supported statically-sized kind",
- val.Type(), val.Kind()))
+ panic(&InvalidTypeError{
+ Type: val.Type(),
+ Err: fmt.Errorf("does not implement binfmt.Marshaler and kind=%v is not a supported statically-sized kind",
+ val.Kind()),
+ })
}
}