Commit a4e8c85b authored by keks's avatar keks

legacy: correctly handle errors

parent 81504552
......@@ -70,9 +70,14 @@ var ErrIncorrectType = errors.New("The command returned a value with a different
// Call invokes the command for the given Request
func (c *Command) Call(req Request, re ResponseEmitter) (err error) {
// 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 {
return err
}
......
......@@ -63,8 +63,7 @@ func (rw *responseWrapper) Output() interface{} {
if err == io.EOF || err == context.Canceled {
return
}
if err != nil {
} else if err != nil {
log.Error(err)
return
}
......@@ -403,9 +402,21 @@ func NewCommand(oldcmd *oldcmds.Command) *Command {
errCh := make(chan error)
go res.Send(errCh)
oldcmd.Run(oldReq, res)
err := <-errCh
if err != nil {
log.Error(err)
select {
case err := <-errCh:
if err != nil {
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
import (
"bytes"
"context"
"fmt"
"io"
"testing"
......@@ -57,6 +58,10 @@ func TestNewCommand(t *testing.T) {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req.SetRootContext(ctx)
buf := bytes.NewBuffer(nil)
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