Skip to content

Commit

Permalink
commands(dht): return final error
Browse files Browse the repository at this point in the history
This error has always been exposed as a value (visible with the `-v` flag) but
we should also be returning it as a final error.

fixes #6032

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Mar 1, 2019
1 parent dc245d6 commit cde41b4
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,18 @@ var provideRefDhtCmd = &cmds.Command{
ctx, cancel := context.WithCancel(req.Context)
ctx, events := notif.RegisterForQueryEvents(ctx)

var provideErr error
go func() {
defer cancel()
var err error
if rec {
err = provideKeysRec(ctx, nd.Routing, nd.DAG, cids)
provideErr = provideKeysRec(ctx, nd.Routing, nd.DAG, cids)
} else {
err = provideKeys(ctx, nd.Routing, cids)
provideErr = provideKeys(ctx, nd.Routing, cids)
}
if err != nil {
if provideErr != nil {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError,
Extra: err.Error(),
Extra: provideErr.Error(),
})
}
}()
Expand All @@ -289,7 +289,7 @@ var provideRefDhtCmd = &cmds.Command{
}
}

return nil
return provideErr
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
Expand Down Expand Up @@ -376,13 +376,15 @@ var findPeerDhtCmd = &cmds.Command{
ctx, cancel := context.WithCancel(req.Context)
ctx, events := notif.RegisterForQueryEvents(ctx)

var findPeerErr error
go func() {
defer cancel()
pi, err := nd.Routing.FindPeer(ctx, pid)
if err != nil {
var pi pstore.PeerInfo
pi, findPeerErr = nd.Routing.FindPeer(ctx, pid)
if findPeerErr != nil {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError,
Extra: err.Error(),
Extra: findPeerErr.Error(),
})
return
}
Expand All @@ -399,7 +401,7 @@ var findPeerDhtCmd = &cmds.Command{
}
}

return nil
return findPeerErr
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
Expand Down Expand Up @@ -458,13 +460,15 @@ Different key types can specify other 'best' rules.
ctx, cancel := context.WithCancel(req.Context)
ctx, events := notif.RegisterForQueryEvents(ctx)

var getErr error
go func() {
defer cancel()
val, err := nd.Routing.GetValue(ctx, dhtkey)
if err != nil {
var val []byte
val, getErr = nd.Routing.GetValue(ctx, dhtkey)
if getErr != nil {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError,
Extra: err.Error(),
Extra: getErr.Error(),
})
} else {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Expand All @@ -480,7 +484,7 @@ Different key types can specify other 'best' rules.
}
}

return nil
return getErr
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
Expand Down Expand Up @@ -552,13 +556,14 @@ NOTE: A value may not exceed 2048 bytes.
ctx, cancel := context.WithCancel(req.Context)
ctx, events := notif.RegisterForQueryEvents(ctx)

var putErr error
go func() {
defer cancel()
err := nd.Routing.PutValue(ctx, key, []byte(data))
if err != nil {
putErr = nd.Routing.PutValue(ctx, key, []byte(data))
if putErr != nil {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError,
Extra: err.Error(),
Extra: putErr.Error(),
})
}
}()
Expand All @@ -569,7 +574,7 @@ NOTE: A value may not exceed 2048 bytes.
}
}

return nil
return putErr
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
Expand Down

0 comments on commit cde41b4

Please sign in to comment.