commands: Made Command run functions return (interface{}, error) instead of setting the values in the response
Matt Bell committed
Nov 10, 2014 at 16:32 UTC
2029168e98ca24bf2cd7efc1fd443ffa13974e01
1 file changed
+15
-2
commands/command.go
+15
-2
@@ -13,7 +13,7 @@ var log = u.Logger("command")
13
14
// Function is the type of function that Commands use.
15
// It reads from the Request, and writes results to the Response.
16
-type Function func(Response, Request)
16
+type Function func(Request) (interface{}, error)
17
18
// Marshaller is a function that takes in a Response, and returns a marshalled []byte
19
// (or an error on failure)
@@ -78,8 +78,21 @@ func (c *Command) Call(req Request) Response {
78
return res
79
}
80
81
- cmd.Run(res, req)
81
+ output, err := cmd.Run(req)
82
+ if err != nil {
83
+ // if returned error is a commands.Error, use its error code
84
+ // otherwise, just default the code to ErrNormal
85
+ var e Error
86
+ e, ok := err.(Error)
87
+ if ok {
88
+ res.SetError(e, e.Code)
89
+ } else {
90
+ res.SetError(err, ErrNormal)
91
+ }
92
+ return res
93
+ }
94
95
+ res.SetOutput(output)
96
return res
97
}
98