Total Lines | 39 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package mobilenig |
||
2 | |||
3 | import ( |
||
4 | "bytes" |
||
5 | "errors" |
||
6 | "net/http" |
||
7 | "strconv" |
||
8 | ) |
||
9 | |||
10 | // ErrorResponse is the response that is returned when there is an API error |
||
11 | type ErrorResponse struct { |
||
12 | Code string `json:"code"` |
||
13 | Description string `json:"description"` |
||
14 | } |
||
15 | |||
16 | // Response captures the http response |
||
17 | type Response struct { |
||
18 | HTTPResponse *http.Response |
||
19 | Body *[]byte |
||
20 | Error *ErrorResponse |
||
21 | } |
||
22 | |||
23 | // Err returns an error if the http request is not successfull |
||
24 | func (r *Response) Err() error { |
||
25 | if r.Error != nil && len(r.Error.Description) > 0 { |
||
26 | return errors.New(r.errorMessage()) |
||
27 | } |
||
28 | return nil |
||
29 | } |
||
30 | |||
31 | func (r *Response) errorMessage() string { |
||
32 | var buf bytes.Buffer |
||
33 | buf.WriteString(strconv.Itoa(r.HTTPResponse.StatusCode)) |
||
34 | buf.WriteString(": ") |
||
35 | buf.WriteString(http.StatusText(r.HTTPResponse.StatusCode)) |
||
36 | buf.WriteString(", Body: ") |
||
37 | buf.Write(*r.Body) |
||
38 | |||
39 | return buf.String() |
||
40 | } |
||
41 |