-
Notifications
You must be signed in to change notification settings - Fork 3
/
Errors.go
71 lines (58 loc) · 1.29 KB
/
Errors.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package HologramGo
// This file contains all Error types used within the package.
import (
"fmt"
)
// Error returned if there was an issue parsing the response body.
type ResponseError struct {
Body string
Code int
}
func NewResponseError(code int, body string) ResponseError {
return ResponseError{Code: code, Body: body}
}
func (e ResponseError) Error() string {
return fmt.Sprintf(
"Unable to handle response (status code %d): `%v`",
e.Code,
e.Body)
}
type Error map[string]interface{}
func (e Error) Code() int64 {
return int64(e["code"].(float64))
}
func (e Error) Message() string {
return e["message"].(string)
}
func (e Error) Error() string {
msg := "Error %v: %v"
return fmt.Sprintf(msg, e.Code(), e.Message())
}
type Errors map[string]interface{}
func (e Errors) Error() string {
var (
msg string = ""
err Error
ok bool
)
if e["errors"] == nil {
return msg
}
for _, val := range e["errors"].([]interface{}) {
if err, ok = val.(map[string]interface{}); ok {
msg += err.Error() + ". "
}
}
return msg
}
func (e Errors) String() string {
return e.Error()
}
func (e Errors) Errors() []Error {
var errs = e["errors"].([]interface{})
var out = make([]Error, len(errs))
for i, val := range errs {
out[i] = Error(val.(map[string]interface{}))
}
return out
}