-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
55 lines (50 loc) · 1.15 KB
/
error.go
File metadata and controls
55 lines (50 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package ftapi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Error contains information about any non 2xx status code response.
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Details []interface{} `json:"details"`
Body string
}
type errorReply struct {
Error *Error `json:"error"`
}
func (e *Error) Error() string {
if e.Message == "" {
return fmt.Sprintf("ftapi: Got HTTP response code %d with body %v", e.Code, e.Body)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "ftapi: Error %d: ", e.Code)
if e.Message != "" {
fmt.Fprintf(&buf, "%s", e.Message)
}
return buf.String()
}
func checkResponse(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return nil
}
errData, err := ioutil.ReadAll(res.Body)
if err == nil {
errType := new(errorReply)
err = json.Unmarshal(errData, errType)
if err == nil && errType.Error != nil {
if errType.Error.Code == 0 {
errType.Error.Code = res.StatusCode
}
errType.Error.Body = string(errData)
return errType.Error
}
}
return &Error{
Code: res.StatusCode,
Body: string(errData),
}
}