tests(2/main) errClient
Discovered this quirk about interfaces. @whyrusleeping @mappum @jbenet License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 13, 2014 at 02:28 UTC
c5e75f91a6a108dd35eaf37d431aeb0099bab936
2 files changed
+27
-4
cmd/ipfs2/main.go
+10
-4
@@ -263,12 +263,18 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
263
}
264
265
func isClientError(err error) bool {
266
+
267
+ // Somewhat suprisingly, the pointer cast fails to recognize commands.Error
268
+ // passed as values, so we check both.
269
+
270
// cast to cmds.Error
267
- cmdErr, ok := err.(*cmds.Error)
268
- if !ok {
269
- return false
271
+ switch e := err.(type) {
272
+ case *cmds.Error:
273
+ return e.Code == cmds.ErrClient
274
+ case cmds.Error:
275
+ return e.Code == cmds.ErrClient
276
}
271
- return cmdErr.Code == cmds.ErrClient
277
+ return false
278
}
279
280
func getConfigRoot(req cmds.Request) (string, error) {
cmd/ipfs2/main_test.go
new
+17
@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import (
4
+ "testing"
5
+
6
+ "github.com/jbenet/go-ipfs/commands"
7
+)
8
+
9
+func TestIsCientErr(t *testing.T) {
10
+ t.Log("Catch both pointers and values")
11
+ if !isClientError(commands.Error{Code: commands.ErrClient}) {
12
+ t.Errorf("misidentified value")
13
+ }
14
+ if !isClientError(&commands.Error{Code: commands.ErrClient}) {
15
+ t.Errorf("misidentified pointer")
16
+ }
17
+}