commands/http: Improved client error handling
Matt Bell committed
Oct 30, 2014 at 21:56 UTC
abcebb0bc2852be1031aa757bb4f953b25e7983b
1 file changed
+22
-4
commands/http/client.go
+22
-4
@@ -1,8 +1,10 @@
1
package http
2
3
import (
4
+ "bytes"
5
"encoding/json"
6
"fmt"
7
+ "io"
8
"net/http"
9
"strings"
10
@@ -65,10 +67,26 @@ func Send(req cmds.Request) (cmds.Response, error) {
67
68
if httpRes.StatusCode >= http.StatusBadRequest {
69
e := cmds.Error{}
68
- err = dec.Decode(&e)
69
- if err != nil {
70
- fmt.Println(err)
71
- return nil, err
70
+
71
+ if httpRes.StatusCode == http.StatusNotFound {
72
+ // handle 404s
73
+ e.Message = "Command not found."
74
+ e.Code = cmds.ErrClient
75
+
76
+ } else if contentType == "text/plain" {
77
+ // handle non-marshalled errors
78
+ buf := bytes.NewBuffer(nil)
79
+ io.Copy(buf, httpRes.Body)
80
+ e.Message = string(buf.Bytes())
81
+ e.Code = cmds.ErrNormal
82
+
83
+ } else {
84
+ // handle marshalled errors
85
+ err = dec.Decode(&e)
86
+ if err != nil {
87
+ fmt.Println(err)
88
+ return nil, err
89
+ }
90
}
91
92
res.SetError(e, e.Code)