diff --git a/xdr3/error.go b/xdr3/error.go index 2a4ed27..53ffbd5 100644 --- a/xdr3/error.go +++ b/xdr3/error.go @@ -106,6 +106,11 @@ type UnmarshalError struct { Err error // The underlying error for IO errors } +// Unwrap returns the underlying error if there is one. +func (e *UnmarshalError) Unwrap() error { + return e.Err +} + // Error satisfies the error interface and prints human-readable errors. func (e *UnmarshalError) Error() string { switch e.ErrorCode { @@ -145,6 +150,11 @@ type MarshalError struct { Err error // The underlying error for IO errors } +// Unwrap returns the underlying error if there is one. +func (e *MarshalError) Unwrap() error { + return e.Err +} + // Error satisfies the error interface and prints human-readable errors. func (e *MarshalError) Error() string { switch e.ErrorCode { diff --git a/xdr3/error_test.go b/xdr3/error_test.go index dc300db..72c8cb0 100644 --- a/xdr3/error_test.go +++ b/xdr3/error_test.go @@ -17,6 +17,8 @@ package xdr_test import ( + "errors" + "io" "testing" . "github.com/stellar/go-xdr/xdr3" @@ -64,6 +66,16 @@ func TestUnmarshalError(t *testing.T) { }, "xdr:test: EOF while decoding 5 bytes - read: 'testval'", }, + { + UnmarshalError{ + ErrorCode: ErrIO, + Func: "test", + Description: "underlying io error", + Value: "testval", + Err: io.ErrShortBuffer, + }, + "xdr:test: underlying io error - read: 'testval'", + }, { UnmarshalError{ ErrorCode: ErrBadEnumValue, @@ -91,6 +103,10 @@ func TestUnmarshalError(t *testing.T) { test.want) continue } + if test.in.Err != nil && !errors.Is(&test.in, test.in.Err) { + t.Errorf("Error #%d\n is not is-able on underlying error", i) + continue + } } } @@ -109,6 +125,16 @@ func TestMarshalError(t *testing.T) { }, "xdr:test: EOF while encoding 5 bytes - wrote: '[1 2]'", }, + { + MarshalError{ + ErrorCode: ErrIO, + Func: "test", + Description: "underlying io error", + Value: []byte{0x01, 0x02}, + Err: io.ErrShortWrite, + }, + "xdr:test: underlying io error - wrote: '[1 2]'", + }, { MarshalError{ ErrorCode: ErrBadEnumValue, @@ -136,5 +162,9 @@ func TestMarshalError(t *testing.T) { test.want) continue } + if test.in.Err != nil && !errors.Is(&test.in, test.in.Err) { + t.Errorf("Error #%d\n is not is-able on underlying error", i) + continue + } } }