-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
163 lines (145 loc) · 7.22 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
package commands
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
)
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")
ErrMustBeWithinRange1366 = errors.New("must be between 1 and 366")
ErrMustBeAtMost500 = errors.New("must be at most 500")
)
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)
}
// 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)
}