-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
96 lines (76 loc) · 1.71 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package resellerclub
import (
"errors"
"strings"
)
var (
ErrMissingParams = errors.New("missing required params")
ErrSomethingWentWrong = errors.New("something went wrong")
ErrNoTLDsSelected = errors.New("No TLDs are selected")
)
type Error struct {
err string
res interface{}
}
func (e Error) Error() string {
return e.err
}
func (e Error) String() string {
return e.err
}
func (e Error) Response() interface{} {
return e.res
}
func (e Error) Is(target error) bool {
return e.err == target.Error()
}
type errorChecker interface {
Err() error
}
type errorResponse struct {
Status string `json:"status"`
Message string `json:"message"`
ErrorValue errorValue `json:"errorvalue"`
}
type errorValue struct {
Error string `json:"error"`
}
func (e *errorResponse) Err() error {
var status = strings.ToLower(e.Status)
if len(status) > 0 {
if len(e.Message) > 0 && status == "error" {
return Error{e.Message, e}
}
return somethingWentWrong(e)
}
if len(e.ErrorValue.Error) > 0 {
return Error{e.ErrorValue.Error, e}
}
return nil
}
func checkResponseError(mapResp map[string]interface{}) error {
status, ok := mapResp["status"].(string)
if ok {
status = strings.ToLower(status)
msg, ok := mapResp["message"].(string)
if ok && len(msg) > 0 && status == "error" {
return Error{msg, mapResp}
}
return somethingWentWrong(mapResp)
}
errorvalue, ok := mapResp["errorvalue"].(map[string]interface{})
if ok {
msg, ok := errorvalue["error"].(string)
if ok && len(msg) > 0 {
return Error{msg, mapResp}
}
return somethingWentWrong(mapResp)
}
return nil
}
func somethingWentWrong(res interface{}) error {
return Error{
ErrSomethingWentWrong.Error(),
res,
}
}