@cryptotaxi247 / kubo / commits / 49792b236

refactored cast errors to use a util

Juan Batiz-Benet committed Nov 11, 2014 at 23:53 UTC 49792b236218f12d987af6ce56827759f4bc8286
11 files changed +39 -30
commands/http/client.go
+4 -6
@@ -3,7 +3,6 @@ package http
3 import (
4 "bytes"
5 "encoding/json"
6 - "errors"
6 "fmt"
7 "io"
8 "net/http"
@@ -11,10 +10,9 @@ import (
10 "strings"
11
12 cmds "github.com/jbenet/go-ipfs/commands"
13 + u "github.com/jbenet/go-ipfs/util"
14 )
15
16 -var castError = errors.New("cast error")
17 -
16 const (
17 ApiUrlFormat = "http://%s%s/%s?%s"
18 ApiPath = "/api/v0" // TODO: make configurable
@@ -70,7 +68,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
68 for k, v := range req.Options() {
69 str, ok := v.(string)
70 if !ok {
73 - return "", nil, castError
71 + return "", nil, u.ErrCast()
72 }
73 query.Set(k, str)
74 }
@@ -87,7 +85,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
85 if argDef.Type == cmds.ArgString {
86 str, ok := arg.(string)
87 if !ok {
90 - return "", nil, castError
88 + return "", nil, u.ErrCast()
89 }
90 query.Add("arg", str)
91
@@ -99,7 +97,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
97 var ok bool
98 inputStream, ok = arg.(io.Reader)
99 if !ok {
102 - return "", nil, castError
100 + return "", nil, u.ErrCast()
101 }
102 }
103 }
commands/request.go
+2 -2
@@ -1,7 +1,6 @@
1 package commands
2
3 import (
4 - "errors"
4 "fmt"
5 "io"
6 "reflect"
@@ -9,6 +8,7 @@ import (
8
9 "github.com/jbenet/go-ipfs/config"
10 "github.com/jbenet/go-ipfs/core"
11 + u "github.com/jbenet/go-ipfs/util"
12 )
13
14 type optMap map[string]interface{}
@@ -176,7 +176,7 @@ func (r *request) ConvertOptions() error {
176 convert := converters[opt.Type]
177 str, ok := v.(string)
178 if !ok {
179 - return errors.New("cast error")
179 + return u.ErrCast()
180 }
181 val, err := convert(str)
182 if err != nil {
core/commands2/add.go
+3 -2
@@ -17,6 +17,7 @@ import (
17 dag "github.com/jbenet/go-ipfs/merkledag"
18 pinning "github.com/jbenet/go-ipfs/pin"
19 ft "github.com/jbenet/go-ipfs/unixfs"
20 + u "github.com/jbenet/go-ipfs/util"
21 )
22
23 // Error indicating the max depth has been exceded.
@@ -41,7 +42,7 @@ MerkleDAG. A smarter partial add with a staging area (like git)
42 remains to be implemented.
43 `,
44 Run: func(req cmds.Request) (interface{}, error) {
44 - var added AddOutput
45 + added := &AddOutput{}
46 n := req.Context().Node
47
48 recursive, err := req.Option("r").Bool()
@@ -167,7 +168,7 @@ remains to be implemented.
168 cmds.Text: func(res cmds.Response) ([]byte, error) {
169 val, ok := res.Output().(*AddOutput)
170 if !ok {
170 - return nil, errors.New("cast error")
171 + return nil, u.ErrCast()
172 }
173
174 var buf bytes.Buffer
core/commands2/block.go
+2 -3
@@ -2,7 +2,6 @@ package commands
2
3 import (
4 "bytes"
5 - "errors"
5 "fmt"
6 "io"
7 "io/ioutil"
@@ -45,7 +44,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
44
45 key, ok := req.Arguments()[0].(string)
46 if !ok {
48 - return nil, errors.New("cast error")
47 + return nil, u.ErrCast()
48 }
49
50 if !u.IsValidHash(key) {
@@ -81,7 +80,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.`,
80
81 in, ok := req.Arguments()[0].(io.Reader)
82 if !ok {
84 - return nil, errors.New("cast error")
83 + return nil, u.ErrCast()
84 }
85
86 data, err := ioutil.ReadAll(in)
core/commands2/bootstrap.go
+2 -2
@@ -1,13 +1,13 @@
1 package commands
2
3 import (
4 - "errors"
4 "fmt"
5 "strings"
6
7 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9
10 + u "github.com/jbenet/go-ipfs/util"
11 cmds "github.com/jbenet/go-ipfs/commands"
12 config "github.com/jbenet/go-ipfs/config"
13 )
@@ -152,7 +152,7 @@ func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error)
152 for _, v := range input {
153 addr, ok := v.(string)
154 if !ok {
155 - return nil, errors.New("cast error")
155 + return nil, u.ErrCast()
156 }
157
158 addrS, peeridS := split(addr)
core/commands2/config.go
+3 -2
@@ -12,6 +12,7 @@ import (
12
13 cmds "github.com/jbenet/go-ipfs/commands"
14 config "github.com/jbenet/go-ipfs/config"
15 + u "github.com/jbenet/go-ipfs/util"
16 )
17
18 type ConfigField struct {
@@ -45,7 +46,7 @@ var configCmd = &cmds.Command{
46
47 key, ok := args[0].(string)
48 if !ok {
48 - return nil, errors.New("cast error")
49 + return nil, u.ErrCast()
50 }
51
52 filename, err := config.Filename(req.Context().ConfigRoot)
@@ -58,7 +59,7 @@ var configCmd = &cmds.Command{
59 var ok bool
60 value, ok = args[1].(string)
61 if !ok {
61 - return nil, errors.New("cast error")
62 + return nil, u.ErrCast()
63 }
64
65 return setConfig(filename, key, value)
core/commands2/internal/slice_util.go
+4 -5
@@ -1,18 +1,17 @@
1 package internal
2
3 import (
4 - "errors"
4 "io"
6 -)
5
8 -var CastErr = errors.New("cast error")
6 + u "github.com/jbenet/go-ipfs/util"
7 +)
8
9 func CastToReaders(slice []interface{}) ([]io.Reader, error) {
10 readers := make([]io.Reader, 0)
11 for _, arg := range slice {
12 reader, ok := arg.(io.Reader)
13 if !ok {
15 - return nil, CastErr
14 + return nil, u.ErrCast()
15 }
16 readers = append(readers, reader)
17 }
@@ -24,7 +23,7 @@ func CastToStrings(slice []interface{}) ([]string, error) {
23 for _, maybe := range slice {
24 str, ok := maybe.(string)
25 if !ok {
27 - return nil, CastErr
26 + return nil, u.ErrCast()
27 }
28 strs = append(strs, str)
29 }
core/commands2/object.go
+7 -6
@@ -8,8 +8,9 @@ import (
8 "io/ioutil"
9
10 cmds "github.com/jbenet/go-ipfs/commands"
11 - "github.com/jbenet/go-ipfs/core"
11 + core "github.com/jbenet/go-ipfs/core"
12 dag "github.com/jbenet/go-ipfs/merkledag"
13 + u "github.com/jbenet/go-ipfs/util"
14 )
15
16 // ErrObjectTooLarge is returned when too much data was read from stdin. current limit 512k
@@ -51,7 +52,7 @@ output is the raw data of the object.
52
53 key, ok := req.Arguments()[0].(string)
54 if !ok {
54 - return nil, errors.New("cast error")
55 + return nil, u.ErrCast()
56 }
57
58 return objectData(n, key)
@@ -71,7 +72,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
72
73 key, ok := req.Arguments()[0].(string)
74 if !ok {
74 - return nil, errors.New("cast error")
75 + return nil, u.ErrCast()
76 }
77
78 return objectLinks(n, key)
@@ -99,7 +100,7 @@ This command outputs data in the following encodings:
100
101 key, ok := req.Arguments()[0].(string)
102 if !ok {
102 - return nil, errors.New("cast error")
103 + return nil, u.ErrCast()
104 }
105
106 object, err := objectGet(n, key)
@@ -151,12 +152,12 @@ Data should be in the format specified by <encoding>.
152
153 input, ok := req.Arguments()[0].(io.Reader)
154 if !ok {
154 - return nil, errors.New("cast error")
155 + return nil, u.ErrCast()
156 }
157
158 encoding, ok := req.Arguments()[1].(string)
159 if !ok {
159 - return nil, errors.New("cast error")
160 + return nil, u.ErrCast()
161 }
162
163 output, err := objectPut(n, input, encoding)
core/commands2/resolve.go
+2 -1
@@ -4,6 +4,7 @@ import (
4 "errors"
5
6 cmds "github.com/jbenet/go-ipfs/commands"
7 + u "github.com/jbenet/go-ipfs/util"
8 )
9
10 var resolveCmd = &cmds.Command{
@@ -49,7 +50,7 @@ Resolve te value of another name:
50 var ok bool
51 name, ok = req.Arguments()[0].(string)
52 if !ok {
52 - return nil, errors.New("cast error")
53 + return nil, u.ErrCast()
54 }
55 }
56
util/testutil/gen.go
+1 -1
@@ -1,8 +1,8 @@
1 package testutil
2
3 import (
4 - "testing"
4 crand "crypto/rand"
5 + "testing"
6
7 "github.com/jbenet/go-ipfs/peer"
8
util/util.go
+9
@@ -8,6 +8,7 @@ import (
8 "math/rand"
9 "os"
10 "path/filepath"
11 + "runtime/debug"
12 "strings"
13 "time"
14
@@ -40,6 +41,14 @@ func TildeExpansion(filename string) (string, error) {
41 return homedir.Expand(filename)
42 }
43
44 +// ErrCast is returned when a cast fails AND the program should not panic.
45 +func ErrCast() error {
46 + debug.PrintStack()
47 + return errCast
48 +}
49 +
50 +var errCast = errors.New("cast error")
51 +
52 // ExpandPathnames takes a set of paths and turns them into absolute paths
53 func ExpandPathnames(paths []string) ([]string, error) {
54 var out []string