-
Notifications
You must be signed in to change notification settings - Fork 22
/
errors.go
207 lines (186 loc) · 9.49 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package commands
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"golang.org/x/exp/maps"
)
var (
ErrIsRequired = errors.New("is required")
ErrMustBePositive = errors.New("must be positive")
ErrMustBePositiveOrZero = errors.New("must be positive or zero")
ErrMustBeNegativeOrZero = errors.New("must be negative or zero")
ErrMustBeLessThan150 = errors.New("must be less than 150")
ErrMustBeAtMost1M = errors.New("must be at most 1000000")
ErrMustBeAtMost100 = errors.New("must be at most 100")
ErrMustBeWithinRange7 = errors.New("must be between -7 and 7")
ErrIsNotValid = errors.New("is not a valid value")
ErrIsNotValidWithOCO = errors.New("is not a valid with one cancel other")
ErrIsNotValidNumber = errors.New("is not a valid number")
ErrIsNotSupported = errors.New("is not supported")
ErrIsUnauthorised = errors.New("is unauthorised")
ErrCannotAmendToGFA = errors.New("cannot amend to time in force GFA")
ErrCannotAmendToGFN = errors.New("cannot amend to time in force GFN")
ErrNonGTTOrderWithExpiry = errors.New("non GTT order with expiry")
ErrGTTOrderWithNoExpiry = errors.New("GTT order without expiry")
ErrIsMismatching = errors.New("is mismatching")
ErrReferenceTooLong = errors.New("reference is too long")
ErrNotAValidInteger = errors.New("not a valid integer")
ErrNotAValidFloat = errors.New("not a valid float")
ErrMustBeLessThan100Chars = errors.New("must be less than 100 characters")
ErrMustBeLessThan200Chars = errors.New("must be less than 200 characters")
ErrMustNotExceed20000Chars = errors.New("must not exceed 20000 characters")
ErrShouldBeHexEncoded = errors.New("should be hex encoded")
ErrSignatureNotVerifiable = errors.New("signature is not verifiable")
ErrInvalidSignature = errors.New("invalid signature")
ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")
ErrEmptyBatchMarketInstructions = errors.New("empty batch market instructions")
ErrIsNotValidVegaPubkey = errors.New("is not a valid vega public key")
ErrIsNotValidEthereumAddress = errors.New("is not a valid ethereum address")
ErrEmptyEthereumCallSpec = errors.New("ethereum call spec is required")
ErrInvalidEthereumAbi = errors.New("is not a valid ethereum abi definition")
ErrInvalidEthereumCallTrigger = errors.New("ethereum call trigger not valid")
ErrInvalidEthereumCallArgs = errors.New("ethereum call arguments not valid")
ErrInvalidEthereumFilters = errors.New("ethereum call filters not valid")
ErrInvalidEthereumCallSpec = errors.New("ethereum call spec is not valid")
ErrMustBeWithinRange01 = errors.New("must be between 0 and 1")
ErrMustBeWithinRange11 = errors.New("must be between -1 and 1")
ErrMustBeLTE1 = errors.New("must be less than or equal to 1")
ErrMustBeGTE1 = errors.New("must be greater than or equal to 1")
ErrMustBeReduceOnly = errors.New("must be reduce only")
ErrExpiryStrategyRequiredWhenExpiresAtSet = errors.New("expiry strategy required when expires_at set")
ErrMustHaveAtLeastOneOfRisesAboveOrFallsBelow = errors.New("must have at least one of rises above or falls below")
ErrMustHaveAStopOrderTrigger = errors.New("must have a stop order trigger")
ErrFallsBelowAndRiseAboveMarketIDMustBeTheSame = errors.New("market ID for falls below and rises above must be the same")
ErrTrailingPercentOffsetMinimalIncrementNotReached = errors.New("trailing percent offset minimal increment must be >= 0.001")
ErrMustBeEmpty = errors.New("must be empty")
ErrMustBeGTEClampLowerBound = errors.New("must be greater than or equal to clamp lower bound")
ErrOneTimeTriggerAllowedMax = errors.New("maximum one time trigger allowed")
ErrMustBeBetween01 = errors.New("must be between 0 (excluded) and 1 (included)")
ErrMustBeGreaterThanEnactmentTimestamp = errors.New("must be greater than proposal_submission.terms.enactment_timestamp")
ErrMustBeLessThen366 = errors.New("must be less then 366")
ErrMustBeAtMost500 = errors.New("must be at most 500")
ErrMustBeSetTo0IfSizeSet = errors.New("must be set to 0 if the property \"order_amendment.size\" is set")
ErrMustBeAtMost3600 = errors.New("must be at most 3600")
ErrMustBeWithinRangeGT0LT20 = errors.New("price range must be strictly greater than 0 and less than or equal to 20")
ErrSizeIsTooLarge = errors.New("size is too large")
ErrCannotSetAllowListWhenTeamIsOpened = errors.New("cannot set allow list when team is opened")
ErrSettingAllowListRequireSettingClosedState = errors.New("setting an allow list requires setting the closed state")
ErrIsLimitedTo32Characters = errors.New("is limited to 32 characters")
ErrIsLimitedTo10Entries = errors.New("is limited to 10 entries")
ErrIsLimitedTo255Characters = errors.New("is limited to 255 characters")
ErrCannotBeBlank = errors.New("cannot be blank")
ErrIsDuplicated = errors.New("is duplicated")
ErrIsDisabled = errors.New("is disabled")
ErrMustBeAtMost250 = errors.New("must be at most 250")
)
type Errors map[string][]error
func NewErrors() Errors {
return Errors{}
}
func (e Errors) Error() string {
if len(e) <= 0 {
return ""
}
propMessages := []string{}
for prop, errs := range e {
errMessages := make([]string, 0, len(errs))
for _, err := range errs {
errMessages = append(errMessages, err.Error())
}
propMessageFmt := fmt.Sprintf("%v (%v)", prop, strings.Join(errMessages, ", "))
propMessages = append(propMessages, propMessageFmt)
}
sort.Strings(propMessages)
return strings.Join(propMessages, ", ")
}
func (e Errors) Empty() bool {
return len(e) == 0
}
// AddForProperty adds an error for a given property.
func (e Errors) AddForProperty(prop string, err error) {
errs, ok := e[prop]
if !ok {
errs = []error{}
}
e[prop] = append(errs, err)
}
// AddPrefix adds prefix to each property.
func (e Errors) AddPrefix(prefix string) Errors {
keys := maps.Keys(e)
for _, key := range keys {
// Skip general error
if key == "*" {
continue
}
e[fmt.Sprintf("%s%s", prefix, key)] = e[key]
delete(e, key)
}
return e
}
// FinalAddForProperty behaves like AddForProperty, but is meant to be called in
// a "return" statement. This helper is usually used for terminal errors.
func (e Errors) FinalAddForProperty(prop string, err error) Errors {
e.AddForProperty(prop, err)
return e
}
// Add adds a general error that is not related to a specific property.
func (e Errors) Add(err error) {
e.AddForProperty("*", err)
}
// FinalAdd behaves like Add, but is meant to be called in a "return" statement.
// This helper is usually used for terminal errors.
func (e Errors) FinalAdd(err error) Errors {
e.Add(err)
return e
}
func (e Errors) Merge(oth Errors) {
if oth == nil {
return
}
for prop, errs := range oth {
for _, err := range errs {
e.AddForProperty(prop, err)
}
}
}
func (e Errors) Get(prop string) []error {
messages, ok := e[prop]
if !ok {
return nil
}
return messages
}
func (e Errors) ErrorOrNil() error {
if len(e) <= 0 {
return nil
}
return e
}
func (e Errors) MarshalJSON() ([]byte, error) {
out := map[string][]string{}
for prop, errs := range e {
messages := []string{}
for _, err := range errs {
messages = append(messages, err.Error())
}
out[prop] = messages
}
return json.Marshal(out)
}