debug(core, datastore, daemon) wrap errors
@jbenet @whyrusleeping @mappum very helpful for tracking down errors. the stack traces are only shown when debug mode is visible. They function best when caught at the source. I propose we use this errors package as a drop-in replacement for fmt.Errorf and errors.New in all of our code, and use errors.Wrap for external errors as they emerge from others' libraries. License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com> Signed-off-by: Brian Tiger Chow <brian.holderchow@gmail.com>
Brian Tiger Chow committed
Nov 16, 2014 at 13:17 UTC
2bbfe4d56fcea4418180115ac34d2195b2c04817
4 files changed
+19
-16
cmd/ipfs2/daemon.go
+2
-1
@@ -6,6 +6,7 @@ import (
6
7
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
9
+ errors "github.com/jbenet/go-ipfs/util/errors"
10
11
manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
12
cmds "github.com/jbenet/go-ipfs/commands"
@@ -35,7 +36,7 @@ the daemon.
36
func daemonFunc(req cmds.Request) (interface{}, error) {
37
lock, err := daemon.Lock(req.Context().ConfigRoot)
38
if err != nil {
38
- return nil, fmt.Errorf("Couldn't obtain lock. Is another daemon already running?")
39
+ return nil, errors.Errorf("Couldn't obtain lock. Is another daemon already running?")
40
}
41
defer lock.Close()
42
core/core.go
+8
-8
@@ -2,7 +2,6 @@ package core
2
3
import (
4
"encoding/base64"
5
- "errors"
5
"fmt"
6
7
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
@@ -15,6 +14,7 @@ import (
14
exchange "github.com/jbenet/go-ipfs/exchange"
15
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
16
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
17
+ mount "github.com/jbenet/go-ipfs/fuse/mount"
18
merkledag "github.com/jbenet/go-ipfs/merkledag"
19
namesys "github.com/jbenet/go-ipfs/namesys"
20
inet "github.com/jbenet/go-ipfs/net"
@@ -26,8 +26,8 @@ import (
26
routing "github.com/jbenet/go-ipfs/routing"
27
dht "github.com/jbenet/go-ipfs/routing/dht"
28
u "github.com/jbenet/go-ipfs/util"
29
- mount "github.com/jbenet/go-ipfs/fuse/mount"
29
ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
30
+ "github.com/jbenet/go-ipfs/util/errors"
31
)
32
33
const IpnsValidatorTag = "ipns"
@@ -102,7 +102,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
102
}()
103
104
if cfg == nil {
105
- return nil, fmt.Errorf("configuration required")
105
+ return nil, errors.Errorf("configuration required")
106
}
107
108
// derive this from a higher context.
@@ -115,14 +115,14 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
115
116
// setup datastore.
117
if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
118
- return nil, err
118
+ return nil, errors.Wrap(err)
119
}
120
121
// setup peerstore + local peer identity
122
n.Peerstore = peer.NewPeerstore()
123
n.Identity, err = initIdentity(&n.Config.Identity, n.Peerstore, online)
124
if err != nil {
125
- return nil, err
125
+ return nil, errors.Wrap(err)
126
}
127
128
// setup online services
@@ -142,12 +142,12 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
142
// setup the network
143
listenAddrs, err := listenAddresses(cfg)
144
if err != nil {
145
- return nil, err
145
+ return nil, errors.Wrap(err)
146
}
147
148
n.Network, err = inet.NewIpfsNetwork(ctx, listenAddrs, n.Identity, n.Peerstore, muxMap)
149
if err != nil {
150
- return nil, err
150
+ return nil, errors.Wrap(err)
151
}
152
n.AddCloserChild(n.Network)
153
@@ -176,7 +176,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
176
// session that simply doesn't return blocks
177
n.Blocks, err = bserv.NewBlockService(n.Datastore, n.Exchange)
178
if err != nil {
179
- return nil, err
179
+ return nil, errors.Wrap(err)
180
}
181
182
n.DAG = merkledag.NewDAGService(n.Blocks)
core/datastore.go
+6
-6
@@ -1,8 +1,6 @@
1
package core
2
3
import (
4
- "fmt"
5
-
4
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5
fsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/fs"
6
ktds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/keytransform"
@@ -12,11 +10,12 @@ import (
10
11
config "github.com/jbenet/go-ipfs/config"
12
u "github.com/jbenet/go-ipfs/util"
13
+ "github.com/jbenet/go-ipfs/util/errors"
14
)
15
16
func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
17
if len(cfg.Type) == 0 {
19
- return nil, fmt.Errorf("config datastore.type required")
18
+ return nil, errors.Errorf("config datastore.type required")
19
}
20
21
switch cfg.Type {
@@ -36,16 +35,17 @@ func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
35
return u.CloserWrap(syncds.MutexWrap(ktd)), nil
36
}
37
39
- return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
38
+ return nil, errors.Errorf("Unknown datastore type: %s", cfg.Type)
39
}
40
41
func makeLevelDBDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
42
if len(cfg.Path) == 0 {
44
- return nil, fmt.Errorf("config datastore.path required for leveldb")
43
+ return nil, errors.Errorf("config datastore.path required for leveldb")
44
}
45
47
- return lds.NewDatastore(cfg.Path, &lds.Options{
46
+ ds, err := lds.NewDatastore(cfg.Path, &lds.Options{
47
// TODO don't import ldbopts. Get from go-datastore.leveldb
48
Compression: ldbopts.NoCompression,
49
})
50
+ return ds, errors.Wrap(err)
51
}
daemon2/daemon.go
+3
-1
@@ -6,13 +6,15 @@ import (
6
7
lock "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
8
"github.com/jbenet/go-ipfs/util"
9
+ "github.com/jbenet/go-ipfs/util/errors"
10
)
11
12
// LockFile is the filename of the daemon lock, relative to config dir
13
const LockFile = "daemon.lock"
14
15
func Lock(confdir string) (io.Closer, error) {
15
- return lock.Lock(path.Join(confdir, LockFile))
16
+ c, err := lock.Lock(path.Join(confdir, LockFile))
17
+ return c, errors.Wrap(err)
18
}
19
20
func Locked(confdir string) bool {