summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Peereboom <marco@conformal.com>2015-05-20 15:33:31 -0500
committerMarco Peereboom <marco@conformal.com>2015-05-20 15:40:18 -0500
commit08a4150305a9fc894d7f4c5a7dc054586cecb8fd (patch)
tree85d1ae186db5349f9d3143c12c12398e53b4bf4a
parentfceca5939f582575efb2606d04a97fde56de1326 (diff)
downloadgo-xdr-08a4150305a9fc894d7f4c5a7dc054586cecb8fd.tar.xz
Add IsIO to detect if an error is of the ErrIO type.
-rw-r--r--xdr2/error.go16
-rw-r--r--xdr2/error_test.go59
2 files changed, 72 insertions, 3 deletions
diff --git a/xdr2/error.go b/xdr2/error.go
index 0832ec9..42079ad 100644
--- a/xdr2/error.go
+++ b/xdr2/error.go
@@ -16,9 +16,7 @@
package xdr
-import (
- "fmt"
-)
+import "fmt"
// ErrorCode identifies a kind of error.
type ErrorCode int
@@ -124,6 +122,18 @@ func unmarshalError(f string, c ErrorCode, desc string, v interface{}, err error
return e
}
+// IsIO returns a boolean indicating whether the error is known to report that
+// the underlying reader or writer encountered an ErrIO.
+func IsIO(err error) bool {
+ switch e := err.(type) {
+ case *UnmarshalError:
+ return e.ErrorCode == ErrIO
+ case *MarshalError:
+ return e.ErrorCode == ErrIO
+ }
+ return false
+}
+
// MarshalError describes a problem encountered while marshaling data.
// Some potential issues are unsupported Go types, attempting to encode more
// opaque data than can be represented by a single opaque XDR entry, and
diff --git a/xdr2/error_test.go b/xdr2/error_test.go
index 906d7f5..af151cc 100644
--- a/xdr2/error_test.go
+++ b/xdr2/error_test.go
@@ -17,6 +17,7 @@
package xdr_test
import (
+ "errors"
"testing"
. "github.com/davecgh/go-xdr/xdr2"
@@ -138,3 +139,61 @@ func TestMarshalError(t *testing.T) {
}
}
}
+
+// TestMarshalError tests the error output for the MarshalError type.
+func TestIOErr(t *testing.T) {
+ tests := []struct {
+ in interface{}
+ want bool
+ }{
+ {
+ &MarshalError{
+ ErrorCode: ErrIO,
+ Func: "test",
+ Description: "EOF while encoding 5 bytes",
+ Value: []byte{0x01, 0x02},
+ },
+ true,
+ },
+ {
+ &MarshalError{
+ ErrorCode: ErrUnsupportedType,
+ Func: "test",
+ Description: "ErrUnsupportedType",
+ Value: []byte{},
+ },
+ false,
+ },
+ {
+ &UnmarshalError{
+ ErrorCode: ErrIO,
+ Func: "test",
+ Description: "EOF while decoding 5 bytes",
+ Value: []byte{0x01, 0x02},
+ },
+ true,
+ },
+ {
+ &UnmarshalError{
+ ErrorCode: ErrUnsupportedType,
+ Func: "test",
+ Description: "ErrUnsupportedType",
+ Value: []byte{},
+ },
+ false,
+ },
+ {
+ errors.New("boom"),
+ false,
+ },
+ }
+
+ for i, test := range tests {
+ result := IsIO(test.in.(error))
+ if result != test.want {
+ t.Errorf("Error #%d\n got: %v want: %v", i, result,
+ test.want)
+ continue
+ }
+ }
+}