/
error.go
53 lines (44 loc) · 904 Bytes
/
error.go
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
package algorithmia
import (
"encoding/json"
"net/http"
"github.com/mitchellh/mapstructure"
)
type Err struct {
Message string `json:"message" mapstructure:"message"`
Stacktrace string `json:"stacktrace" mapstructure:"stacktrace"`
}
func (e *Err) Error() string {
return e.Message
}
func errFromJsonData(data []byte) (*Err, error) {
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
}
if mm, ok := m["error"]; ok {
var err Err
if e := mapstructure.Decode(mm, &err); e != nil {
return nil, e
}
return &err, nil
}
return nil, nil
}
func errorFromJsonData(data []byte) error {
e1, e2 := errFromJsonData(data)
if e2 != nil {
return e2
}
if e1 != nil {
return e1
}
return nil
}
func errorFromResponse(resp *http.Response) error {
b, err := getRaw(resp)
if err != nil {
return err
}
return errorFromJsonData(b)
}