summaryrefslogtreecommitdiff
path: root/lib/binstruct/unmarshal.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/binstruct/unmarshal.go')
-rw-r--r--lib/binstruct/unmarshal.go23
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/binstruct/unmarshal.go b/lib/binstruct/unmarshal.go
index c545137..61c2a4a 100644
--- a/lib/binstruct/unmarshal.go
+++ b/lib/binstruct/unmarshal.go
@@ -5,6 +5,7 @@
package binstruct
import (
+ "errors"
"fmt"
"reflect"
)
@@ -15,7 +16,15 @@ type Unmarshaler interface {
func Unmarshal(dat []byte, dstPtr any) (int, error) {
if unmar, ok := dstPtr.(Unmarshaler); ok {
- return unmar.UnmarshalBinary(dat)
+ n, err := unmar.UnmarshalBinary(dat)
+ if err != nil {
+ err = &UnmarshalError{
+ Type: reflect.TypeOf(dstPtr),
+ Method: "UnmarshalBinary",
+ Err: err,
+ }
+ }
+ return n, err
}
return UnmarshalWithoutInterface(dat, dstPtr)
}
@@ -23,7 +32,10 @@ func Unmarshal(dat []byte, dstPtr any) (int, error) {
func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) {
_dstPtr := reflect.ValueOf(dstPtr)
if _dstPtr.Kind() != reflect.Ptr {
- return 0, fmt.Errorf("not a pointer: %v", _dstPtr.Type())
+ panic(&InvalidTypeError{
+ Type: _dstPtr.Type(),
+ Err: errors.New("not a pointer"),
+ })
}
dst := _dstPtr.Elem()
@@ -52,7 +64,10 @@ func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) {
case reflect.Struct:
return getStructHandler(dst.Type()).Unmarshal(dat, dst)
default:
- panic(fmt.Errorf("type=%v does not implement binfmt.Unmarshaler and kind=%v is not a supported statically-sized kind",
- dst.Type(), dst.Kind()))
+ panic(&InvalidTypeError{
+ Type: _dstPtr.Type(),
+ Err: fmt.Errorf("does not implement binfmt.Unmarshaler and kind=%v is not a supported statically-sized kind",
+ dst.Kind()),
+ })
}
}