Unverified Commit 34933316 authored by keks's avatar keks Committed by GitHub

Merge pull request #34 from ipfs/fix/legacy-errors

Fix error and cancelation handling in legacy
parents 90f810b3 5c0551ca
...@@ -70,9 +70,14 @@ var ErrIncorrectType = errors.New("The command returned a value with a different ...@@ -70,9 +70,14 @@ var ErrIncorrectType = errors.New("The command returned a value with a different
// Call invokes the command for the given Request // Call invokes the command for the given Request
func (c *Command) Call(req Request, re ResponseEmitter) (err error) { func (c *Command) Call(req Request, re ResponseEmitter) (err error) {
// we need the named return parameter so we can change the value from defer() // we need the named return parameter so we can change the value from defer()
defer re.Close() defer func() {
if err == nil {
re.Close()
}
}()
cmd, err := c.Get(req.Path()) var cmd *Command
cmd, err = c.Get(req.Path())
if err != nil { if err != nil {
return err return err
} }
......
...@@ -63,8 +63,7 @@ func (rw *responseWrapper) Output() interface{} { ...@@ -63,8 +63,7 @@ func (rw *responseWrapper) Output() interface{} {
if err == io.EOF || err == context.Canceled { if err == io.EOF || err == context.Canceled {
return return
} } else if err != nil {
if err != nil {
log.Error(err) log.Error(err)
return return
} }
...@@ -403,9 +402,27 @@ func NewCommand(oldcmd *oldcmds.Command) *Command { ...@@ -403,9 +402,27 @@ func NewCommand(oldcmd *oldcmds.Command) *Command {
errCh := make(chan error) errCh := make(chan error)
go res.Send(errCh) go res.Send(errCh)
oldcmd.Run(oldReq, res) oldcmd.Run(oldReq, res)
err := <-errCh select {
if err != nil { case err := <-errCh:
log.Error(err) if err != nil {
select {
case <-req.Context().Done():
err = cmdkit.Error{Message: req.Context().Err().Error(), Code: cmdkit.ErrNormal}
default:
}
if e, ok := err.(*cmdkit.Error); ok {
err = *e
}
if e, ok := err.(cmdkit.Error); ok {
re.SetError(e.Message, e.Code)
} else {
re.SetError(err.Error(), cmdkit.ErrNormal)
}
}
case <-req.Context().Done():
re.SetError(req.Context().Err(), cmdkit.ErrNormal)
} }
} }
} }
......
...@@ -2,6 +2,7 @@ package cmds ...@@ -2,6 +2,7 @@ package cmds
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"testing" "testing"
...@@ -57,6 +58,10 @@ func TestNewCommand(t *testing.T) { ...@@ -57,6 +58,10 @@ func TestNewCommand(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req.SetRootContext(ctx)
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
testCmd := root.Subcommand("test") testCmd := root.Subcommand("test")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment