@cryptotaxi247 / kubo / commits / b4d94c93c

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>

Steven Allen committed Mar 1, 2019 at 10:54 UTC b4d94c93c95dbf93bb592cdc259b763e03c38980
2 files changed +30 -25
core/commands/dht.go
+23 -18
@@ -267,18 +267,18 @@ var provideRefDhtCmd = &cmds.Command{
267 ctx, cancel := context.WithCancel(req.Context)
268 ctx, events := notif.RegisterForQueryEvents(ctx)
269
270 + var provideErr error
271 go func() {
272 defer cancel()
272 - var err error
273 if rec {
274 - err = provideKeysRec(ctx, nd.Routing, nd.DAG, cids)
274 + provideErr = provideKeysRec(ctx, nd.Routing, nd.DAG, cids)
275 } else {
276 - err = provideKeys(ctx, nd.Routing, cids)
276 + provideErr = provideKeys(ctx, nd.Routing, cids)
277 }
278 - if err != nil {
278 + if provideErr != nil {
279 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
280 Type: notif.QueryError,
281 - Extra: err.Error(),
281 + Extra: provideErr.Error(),
282 })
283 }
284 }()
@@ -289,7 +289,7 @@ var provideRefDhtCmd = &cmds.Command{
289 }
290 }
291
292 - return nil
292 + return provideErr
293 },
294 Encoders: cmds.EncoderMap{
295 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
@@ -376,13 +376,15 @@ var findPeerDhtCmd = &cmds.Command{
376 ctx, cancel := context.WithCancel(req.Context)
377 ctx, events := notif.RegisterForQueryEvents(ctx)
378
379 + var findPeerErr error
380 go func() {
381 defer cancel()
381 - pi, err := nd.Routing.FindPeer(ctx, pid)
382 - if err != nil {
382 + var pi pstore.PeerInfo
383 + pi, findPeerErr = nd.Routing.FindPeer(ctx, pid)
384 + if findPeerErr != nil {
385 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
386 Type: notif.QueryError,
385 - Extra: err.Error(),
387 + Extra: findPeerErr.Error(),
388 })
389 return
390 }
@@ -399,7 +401,7 @@ var findPeerDhtCmd = &cmds.Command{
401 }
402 }
403
402 - return nil
404 + return findPeerErr
405 },
406 Encoders: cmds.EncoderMap{
407 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.
460 ctx, cancel := context.WithCancel(req.Context)
461 ctx, events := notif.RegisterForQueryEvents(ctx)
462
463 + var getErr error
464 go func() {
465 defer cancel()
463 - val, err := nd.Routing.GetValue(ctx, dhtkey)
464 - if err != nil {
466 + var val []byte
467 + val, getErr = nd.Routing.GetValue(ctx, dhtkey)
468 + if getErr != nil {
469 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
470 Type: notif.QueryError,
467 - Extra: err.Error(),
471 + Extra: getErr.Error(),
472 })
473 } else {
474 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
@@ -480,7 +484,7 @@ Different key types can specify other 'best' rules.
484 }
485 }
486
483 - return nil
487 + return getErr
488 },
489 Encoders: cmds.EncoderMap{
490 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.
556 ctx, cancel := context.WithCancel(req.Context)
557 ctx, events := notif.RegisterForQueryEvents(ctx)
558
559 + var putErr error
560 go func() {
561 defer cancel()
557 - err := nd.Routing.PutValue(ctx, key, []byte(data))
558 - if err != nil {
562 + putErr = nd.Routing.PutValue(ctx, key, []byte(data))
563 + if putErr != nil {
564 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
565 Type: notif.QueryError,
561 - Extra: err.Error(),
566 + Extra: putErr.Error(),
567 })
568 }
569 }()
@@ -569,7 +574,7 @@ NOTE: A value may not exceed 2048 bytes.
574 }
575 }
576
572 - return nil
577 + return putErr
578 },
579 Encoders: cmds.EncoderMap{
580 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
test/sharness/t0170-dht.sh
+7 -7
@@ -51,15 +51,15 @@ test_dht() {
51 test_fsh cat putted
52 '
53
54 - test_expect_failure 'put with bad keys returns error (issue #4611)' '
55 - ! ipfsi 0 dht put "foo" "bar" &&
56 - ! ipfsi 0 dht put "/pk/foo" "bar" &&
57 - ! ipfsi 0 dht put "/ipns/foo" "bar"
54 + test_expect_success 'put with bad keys returns error (issue #4611)' '
55 + test_must_fail ipfsi 0 dht put "foo" "bar" &&
56 + test_must_fail ipfsi 0 dht put "/pk/foo" "bar" &&
57 + test_must_fail ipfsi 0 dht put "/ipns/foo" "bar"
58 '
59
60 - test_expect_failure 'get with bad keys (issue #4611)' '
61 - ! ipfsi 0 dht get "foo" &&
62 - ! ipfsi 0 dht get "/pk/foo"
60 + test_expect_success 'get with bad keys (issue #4611)' '
61 + test_must_fail ipfsi 0 dht get "foo" &&
62 + test_must_fail ipfsi 0 dht get "/pk/foo"
63 '
64
65 test_expect_success "add a ref so we can find providers for it" '