mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 03:17:43 +08:00
commands: Made Command run functions return (interface{}, error) instead of setting the values in the response
This commit is contained in:
parent
352c29418f
commit
2029168e98
@ -13,7 +13,7 @@ var log = u.Logger("command")
|
||||
|
||||
// Function is the type of function that Commands use.
|
||||
// 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
|
||||
// (or an error on failure)
|
||||
@ -78,8 +78,21 @@ func (c *Command) Call(req Request) Response {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user