mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-28 05:47:51 +08:00
commands(dht): return final error
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 fixes #4611 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
This commit is contained in:
parent
64dee03911
commit
b4d94c93c9
@ -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, ¬if.QueryEvent{
|
||||
Type: notif.QueryError,
|
||||
Extra: err.Error(),
|
||||
Extra: provideErr.Error(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
@ -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 {
|
||||
@ -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, ¬if.QueryEvent{
|
||||
Type: notif.QueryError,
|
||||
Extra: err.Error(),
|
||||
Extra: findPeerErr.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@ -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 {
|
||||
@ -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, ¬if.QueryEvent{
|
||||
Type: notif.QueryError,
|
||||
Extra: err.Error(),
|
||||
Extra: getErr.Error(),
|
||||
})
|
||||
} else {
|
||||
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{
|
||||
@ -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 {
|
||||
@ -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, ¬if.QueryEvent{
|
||||
Type: notif.QueryError,
|
||||
Extra: err.Error(),
|
||||
Extra: putErr.Error(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
@ -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 {
|
||||
|
||||
@ -51,15 +51,15 @@ test_dht() {
|
||||
test_fsh cat putted
|
||||
'
|
||||
|
||||
test_expect_failure 'put with bad keys returns error (issue #4611)' '
|
||||
! ipfsi 0 dht put "foo" "bar" &&
|
||||
! ipfsi 0 dht put "/pk/foo" "bar" &&
|
||||
! ipfsi 0 dht put "/ipns/foo" "bar"
|
||||
test_expect_success 'put with bad keys returns error (issue #4611)' '
|
||||
test_must_fail ipfsi 0 dht put "foo" "bar" &&
|
||||
test_must_fail ipfsi 0 dht put "/pk/foo" "bar" &&
|
||||
test_must_fail ipfsi 0 dht put "/ipns/foo" "bar"
|
||||
'
|
||||
|
||||
test_expect_failure 'get with bad keys (issue #4611)' '
|
||||
! ipfsi 0 dht get "foo" &&
|
||||
! ipfsi 0 dht get "/pk/foo"
|
||||
test_expect_success 'get with bad keys (issue #4611)' '
|
||||
test_must_fail ipfsi 0 dht get "foo" &&
|
||||
test_must_fail ipfsi 0 dht get "/pk/foo"
|
||||
'
|
||||
|
||||
test_expect_success "add a ref so we can find providers for it" '
|
||||
|
||||
Loading…
Reference in New Issue
Block a user