-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
138 lines (109 loc) · 2.52 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package valuefirst
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"net/http"
"strconv"
)
// List of errors in the valuefirst package.
var (
ErrEmptyUsername = errors.New("empty username")
ErrEmptyPassword = errors.New("empty password")
ErrDecodeVarIsNotPtr = errors.New("the decode variable is not a pointer")
ErrNilRequest = errors.New("nil request")
)
func formatUnknown(resp *http.Response) (err error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = &UnknownError{
Message: string(b),
}
return
}
// UnknownError is an error that is not defined by the documentation from the ValueFirst.
type UnknownError struct {
Message interface{} `json:"message"`
}
// Error implements the error interface.
func (e UnknownError) Error() string {
return fmt.Sprintf(
"unknown error ValueFirst: message: %v",
e.Message,
)
}
// ErrorList is a list of errors.
type ErrorList []error
// Error implements the error interface.
func (e *ErrorList) Error() (res string) {
byteSlice, _ := json.Marshal(e)
res = string(byteSlice)
return
}
// Append appends error to the ErrorList.
func (e *ErrorList) Append(err error) {
if err == nil {
return
}
*e = append(*e, err)
}
// ErrorCode represents the error code used in the ValueFirst platform.
type ErrorCode string
// Integer returns the integer value of the error code.
func (e ErrorCode) Integer() int {
res, _ := strconv.Atoi(string(e))
return res
}
// List of error codes in the ValueFirst.
const (
// General
ErrCodeGeneralSuccess ErrorCode = "0"
ErrCodeGeneralNotConformDTD ErrorCode = "65535"
// Message Post
ErrCodeMessageInvalidSenderID ErrorCode = "28680"
ErrCodeMessageInvalidMessage ErrorCode = "28681"
// Status Request
ErrCodeStatusRequestSuccess ErrorCode = "8448"
// Scheduler Related
ErrCodeSchedulerSuccess ErrorCode = "13568"
)
var ignoredErrorCode = map[ErrorCode]bool{
ErrCodeGeneralSuccess: true,
ErrCodeStatusRequestSuccess: true,
ErrCodeSchedulerSuccess: true,
}
func filterError(in *ResponseMessageAckGUIDError) (err error) {
if in == nil {
return
}
_, ok := ignoredErrorCode[in.Code]
if ok {
return
}
err = in
return
}
func filterErrors(ins *ResponseMessageAckGUIDErrors) (err error) {
if ins == nil {
return
}
var (
newList ResponseMessageAckGUIDErrors
tempErr error
)
for _, in := range *ins {
tempErr = filterError(&in)
if tempErr == nil {
continue
}
newList = append(newList, in)
}
if len(newList) <= 0 {
return
}
err = &newList
return
}