refactor(ipfs2/main) output response
Brian Tiger Chow committed
Nov 10, 2014 at 22:58 UTC
fa5ca3f27fa93940aabeaae33c05f71907fa36c9
1 file changed
+16
-5
cmd/ipfs2/main.go
+16
-5
@@ -1,6 +1,7 @@
1
package main
2
3
import (
4
+ "errors"
5
"fmt"
6
"io"
7
"os"
@@ -80,7 +81,11 @@ func run() error {
81
if err != nil {
82
return err
83
}
83
- outputResponse(res, root)
84
+
85
+ err = outputResponse(res, root)
86
+ if err != nil {
87
+ return err
88
+ }
89
90
return nil
91
}
@@ -205,10 +210,15 @@ func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
210
return res, nil
211
}
212
208
-func outputResponse(res cmds.Response, root *cmds.Command) {
213
+func outputResponse(res cmds.Response, root *cmds.Command) error {
214
if res.Error() != nil {
215
fmt.Printf(errorFormat, res.Error().Error())
216
217
+ if res.Error().Code != cmds.ErrClient {
218
+ return res.Error()
219
+ }
220
+
221
+ // if this is a client error, we try to display help text
222
if res.Error().Code == cmds.ErrClient {
223
helpText, err := cmdsCli.HelpText("ipfs", root, res.Request().Path())
224
if err != nil {
@@ -218,16 +228,17 @@ func outputResponse(res cmds.Response, root *cmds.Command) {
228
}
229
}
230
221
- exit(1)
231
+ emptyErr := errors.New("") // already displayed error text, but want to exit(1)
232
+ return emptyErr
233
}
234
235
out, err := res.Reader()
236
if err != nil {
226
- fmt.Println(err.Error())
227
- return
237
+ return err
238
}
239
240
io.Copy(os.Stdout, out)
241
+ return nil
242
}
243
244
func getConfigRoot(req cmds.Request) (string, error) {