forked from EddyTravels/smooch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
49 lines (41 loc) · 870 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
package smooch
import (
"encoding/json"
"fmt"
"net/http"
)
// ResponseData defines data for every response
type ResponseData struct {
HTTPCode int
Flag string
}
type SmoochError struct {
message string
code int
}
func (e *SmoochError) Code() int {
return e.code
}
func (e *SmoochError) Error() string {
return e.message
}
func checkSmoochError(r *http.Response) (*ResponseData, error) {
var errorPayload ErrorPayload
decodeErr := json.NewDecoder(r.Body).Decode(&errorPayload)
if decodeErr != nil {
return nil, decodeErr
}
err := &SmoochError{
message: fmt.Sprintf("StatusCode: %d Code: %s Message: %s",
r.StatusCode,
errorPayload.Details.Code,
errorPayload.Details.Description,
),
code: r.StatusCode,
}
respData := &ResponseData{
HTTPCode: r.StatusCode,
Flag: errorPayload.Details.Code,
}
return respData, err
}