Commit 2029168e authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands: Made Command run functions return (interface{}, error) instead of...

commands: Made Command run functions return (interface{}, error) instead of setting the values in the response
parent 352c2941
...@@ -13,7 +13,7 @@ var log = u.Logger("command") ...@@ -13,7 +13,7 @@ var log = u.Logger("command")
// Function is the type of function that Commands use. // Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response. // It reads from the Request, and writes results to the Response.
type Function func(Response, Request) type Function func(Request) (interface{}, error)
// Marshaller is a function that takes in a Response, and returns a marshalled []byte // Marshaller is a function that takes in a Response, and returns a marshalled []byte
// (or an error on failure) // (or an error on failure)
...@@ -78,8 +78,21 @@ func (c *Command) Call(req Request) Response { ...@@ -78,8 +78,21 @@ func (c *Command) Call(req Request) Response {
return res return res
} }
cmd.Run(res, req) output, err := cmd.Run(req)
if err != nil {
// if returned error is a commands.Error, use its error code
// otherwise, just default the code to ErrNormal
var e Error
e, ok := err.(Error)
if ok {
res.SetError(e, e.Code)
} else {
res.SetError(err, ErrNormal)
}
return res
}
res.SetOutput(output)
return res return res
} }
......
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