-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
214 lines (207 loc) · 10.4 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
208
209
210
211
212
213
214
// 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 api
import (
types "code.vegaprotocol.io/vega/protos/vega"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// API Errors and descriptions.
var (
// ErrChainNotConnected signals to the user that he cannot access a given endpoint
// which require the chain, but the chain is actually offline.
ErrChainNotConnected = errors.New("chain not connected")
// ErrChannelClosed signals that the channel streaming data is closed.
ErrChannelClosed = errors.New("channel closed")
// ErrEmptyMissingMarketID signals to the caller that the request expected a
// market id but the field is missing or empty.
ErrEmptyMissingMarketID = errors.New("empty or missing market ID")
// ErrEmptyMissingOrderID signals to the caller that the request expected an
// order id but the field is missing or empty.
ErrEmptyMissingOrderID = errors.New("empty or missing order ID")
// ErrEmptyMissingOrderReference signals to the caller that the request expected an
// order reference but the field is missing or empty.
ErrEmptyMissingOrderReference = errors.New("empty or missing order reference")
// ErrEmptyMissingPartyID signals to the caller that the request expected a
// party id but the field is missing or empty.
ErrEmptyMissingPartyID = errors.New("empty or missing party ID")
// ErrEmptyMissingSinceTimestamp signals to the caller that the request expected a
// timestamp but the field is missing or empty.
ErrEmptyMissingSinceTimestamp = errors.New("empty or missing since-timestamp")
// ErrServerShutdown signals to the client that the server is shutting down.
ErrServerShutdown = errors.New("server shutdown")
// ErrStreamClosed signals to the users that the grpc stream is closing.
ErrStreamClosed = errors.New("stream closed")
// ErrStreamInternal signals to the users that the grpc stream has an internal problem.
ErrStreamInternal = errors.New("internal stream failure")
// ErrNotMapped is when an error cannot be found in the current error map/lookup table.
ErrNotMapped = errors.New("error not found in error lookup table")
// ErrInvalidMarketID signals that the market ID does not exists.
ErrInvalidMarketID = errors.New("invalid market ID")
// ErrMissingOrder signals that the actual payload is expected to contains an order.
ErrMissingOrder = errors.New("missing order in request payload")
// ErrMissingPartyID signals that the payload is expected to contain a party id.
ErrMissingPartyID = errors.New("missing party id")
// ErrMalformedRequest signals that the request was malformed.
ErrMalformedRequest = errors.New("malformed request")
// ErrMissingAsset signals that an asset was required but not specified.
ErrMissingAsset = errors.New("missing asset")
// ErrSubmitOrder is returned when submitting an order fails for some reason.
ErrSubmitOrder = errors.New("submit order failure")
// ErrAmendOrder is returned when amending an order fails for some reason.
ErrAmendOrder = errors.New("amend order failure")
// ErrCancelOrder is returned when cancelling an order fails for some reason.
ErrCancelOrder = errors.New("cancel order failure")
// OrderService...
ErrOrderServiceGetByMarket = errors.New("failed to get orders for market")
ErrOrderServiceGetByMarketAndID = errors.New("failed to get orders for market and ID")
ErrOrderServiceGetByParty = errors.New("failed to get orders for party")
ErrOrderServiceGetByReference = errors.New("failed to get orders for reference")
ErrMissingOrderIDParameter = errors.New("missing orderID parameter")
ErrOrderNotFound = errors.New("order not found")
// TradeService...
ErrTradeServiceGetByParty = errors.New("failed to get trades for party")
ErrTradeServiceGetByMarket = errors.New("failed to get trades for market")
ErrTradeServiceGetPositionsByParty = errors.New("failed to get positions for party")
ErrTradeServiceGetByOrderID = errors.New("failed to get trades for order ID")
// MarketService...
ErrMarketServiceGetMarkets = errors.New("failed to get markets")
ErrMarketServiceGetByID = errors.New("failed to get market for ID")
ErrMarketServiceGetDepth = errors.New("failed to get market depth")
ErrMarketServiceGetMarketData = errors.New("failed to get market data")
// AccountService...
ErrAccountServiceGetMarketAccounts = errors.New("failed to get market accounts")
// AccountService...
ErrAccountServiceGetFeeInfrastructureAccounts = errors.New("failed to get fee infrastructure accounts")
ErrAccountServiceGetPartyAccounts = errors.New("failed to get party accounts")
// RiskService...
ErrRiskServiceGetMarginLevelsByID = errors.New("failed to get margin levels")
// CandleService...
ErrCandleServiceGetCandles = errors.New("failed to get candles")
// PartyService...
ErrPartyServiceGetAll = errors.New("failed to get parties")
ErrPartyServiceGetByID = errors.New("failed to get party for ID")
// TimeService...
ErrTimeServiceGetTimeNow = errors.New("failed to get time now")
// Blockchain...
ErrBlockchainBacklogLength = errors.New("failed to get backlog length from blockchain")
ErrBlockchainNetworkInfo = errors.New("failed to get network info from blockchain")
ErrBlockchainGenesisTime = errors.New("failed to get genesis time from blockchain")
ErrBlockchainChainID = errors.New("failed to get chain ID from blockchain")
// ErrMissingProposalID returned if proposal with this id is missing.
ErrMissingProposalID = errors.New("missing proposal id")
// ErrMissingProposalReference returned if proposal with this reference is not found.
ErrMissingProposalReference = errors.New("failed to find proposal with the reference")
// ErrMissingWithdrawalID is returned when the ID is missing from the request.
ErrMissingWithdrawalID = errors.New("missing withdrawal ID")
// ErrMissingOracleSpecID is returned when the ID is missing from the request.
ErrMissingOracleSpecID = errors.New("missing oracle spec ID")
// ErrMissingDepositID is returned when the ID is missing from the request.
ErrMissingDepositID = errors.New("missing deposit ID")
)
// errorMap contains a mapping between errors and Vega numeric error codes.
var errorMap = map[string]int32{
// General
ErrNotMapped.Error(): 10000,
ErrChainNotConnected.Error(): 10001,
ErrChannelClosed.Error(): 10002,
ErrEmptyMissingMarketID.Error(): 10003,
ErrEmptyMissingOrderID.Error(): 10004,
ErrEmptyMissingOrderReference.Error(): 10005,
ErrEmptyMissingPartyID.Error(): 10006,
ErrEmptyMissingSinceTimestamp.Error(): 10007,
ErrStreamClosed.Error(): 10008,
ErrServerShutdown.Error(): 10009,
ErrStreamInternal.Error(): 10010,
ErrInvalidMarketID.Error(): 10011,
ErrMissingOrder.Error(): 10012,
ErrMissingPartyID.Error(): 10014,
ErrMalformedRequest.Error(): 10015,
ErrMissingAsset.Error(): 10017,
ErrSubmitOrder.Error(): 10018,
ErrAmendOrder.Error(): 10019,
ErrCancelOrder.Error(): 10020,
// Orders
ErrOrderServiceGetByMarket.Error(): 20001,
ErrOrderServiceGetByMarketAndID.Error(): 20002,
ErrOrderServiceGetByParty.Error(): 20003,
ErrOrderServiceGetByReference.Error(): 20004,
// Markets
ErrMarketServiceGetMarkets.Error(): 30001,
ErrMarketServiceGetByID.Error(): 30002,
ErrMarketServiceGetDepth.Error(): 30003,
ErrMarketServiceGetMarketData.Error(): 30004,
// Trades
ErrTradeServiceGetByMarket.Error(): 40001,
ErrTradeServiceGetByParty.Error(): 40002,
ErrTradeServiceGetPositionsByParty.Error(): 40003,
ErrTradeServiceGetByOrderID.Error(): 40004,
// Parties
ErrPartyServiceGetAll.Error(): 50001,
ErrPartyServiceGetByID.Error(): 50002,
// Candles
ErrCandleServiceGetCandles.Error(): 60001,
// Risk
ErrRiskServiceGetMarginLevelsByID.Error(): 70001,
// Accounts
ErrAccountServiceGetMarketAccounts.Error(): 80001,
ErrAccountServiceGetPartyAccounts.Error(): 80002,
ErrMissingWithdrawalID.Error(): 80003,
ErrMissingDepositID.Error(): 80004,
// Blockchain client
ErrBlockchainBacklogLength.Error(): 90001,
ErrBlockchainNetworkInfo.Error(): 90002,
ErrBlockchainGenesisTime.Error(): 90003,
// End of mapping
}
// ErrorMap returns a map of error to code, which is a mapping between
// API errors and Vega API specific numeric codes.
func ErrorMap() map[string]int32 {
return errorMap
}
// apiError is a helper function to build the Vega specific Error Details that
// can be returned by gRPC API and therefore also REST, GraphQL will be mapped too.
// It takes a standardised grpcCode, a Vega specific apiError, and optionally one
// or more internal errors (error from the core, rather than API).
func apiError(grpcCode codes.Code, apiError error, innerErrors ...error) error {
s := status.Newf(grpcCode, "%v error", grpcCode)
// Create the API specific error detail for error e.g. missing party ID
detail := types.ErrorDetail{
Message: apiError.Error(),
}
// Lookup the API specific error in the table, return not found/not mapped
// if a code has not yet been added to the map, can happen if developer misses
// a step, periodic checking/ownership of API package can keep this up to date.
vegaCode, found := errorMap[apiError.Error()]
if found {
detail.Code = vegaCode
} else {
detail.Code = errorMap[ErrNotMapped.Error()]
}
// If there is an inner error (and possibly in the future, a config to turn this
// level of detail on/off) then process and append to inner.
first := true
for _, err := range innerErrors {
if !first {
detail.Inner += ", "
}
detail.Inner += err.Error()
first = false
}
// Pack the Vega domain specific errorDetails into the status returned by gRPC domain.
s, _ = s.WithDetails(&detail)
return s.Err()
}