common: update Errors type (#1129)

* common: adjust common error slice to allow multi errors.Is matching and conform to interface better

* zb: forgot to save?

* linties: fixies

* linties: word change as well.

* nitters: glorious

* buts

* nitters: fix glorious bug

* Update common/common.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* nitters: shifty

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2023-02-20 10:48:24 +11:00
committed by GitHub
parent ffea386f81
commit d2561402c4
28 changed files with 325 additions and 270 deletions

View File

@@ -237,10 +237,10 @@ func (s *Service) Update(incoming *Holdings, creds *Credentials) error {
s.exchangeAccounts[exch] = accounts
}
var errs common.Errors
var errs error
for x := range incoming.Accounts {
if !incoming.Accounts[x].AssetType.IsValid() {
errs = append(errs, fmt.Errorf("cannot load sub account holdings for %s [%s] %w",
errs = common.AppendError(errs, fmt.Errorf("cannot load sub account holdings for %s [%s] %w",
incoming.Accounts[x].ID,
incoming.Accounts[x].AssetType,
asset.ErrNotSupported))
@@ -295,11 +295,7 @@ func (s *Service) Update(incoming *Holdings, creds *Credentials) error {
return err
}
if errs != nil {
return errs
}
return nil
return errs
}
// load checks to see if there is a change from incoming balance, if there is a

View File

@@ -1544,7 +1544,6 @@ func (b *Bitfinex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
// Subscribe sends a websocket message to receive data from the channel
func (b *Bitfinex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
checksum := make(map[string]interface{})
checksum["event"] = "conf"
checksum["flags"] = bitfinexChecksumFlag + bitfinexWsSequenceFlag
@@ -1553,6 +1552,7 @@ func (b *Bitfinex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) e
return err
}
var errs error
for i := range channelsToSubscribe {
req := make(map[string]interface{})
req["event"] = "subscribe"
@@ -1564,20 +1564,17 @@ func (b *Bitfinex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) e
err := b.Websocket.Conn.SendJSONMessage(req)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
b.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (b *Bitfinex) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
req := make(map[string]interface{})
req["event"] = "unsubscribe"
@@ -1589,15 +1586,12 @@ func (b *Bitfinex) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscriptio
err := b.Websocket.Conn.SendJSONMessage(req)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
b.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// WsSendAuth sends a authenticated event payload

View File

@@ -207,7 +207,7 @@ func (b *Bitstamp) generateDefaultSubscriptions() ([]stream.ChannelSubscription,
// Subscribe sends a websocket message to receive data from the channel
func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
req := websocketEventRequest{
Event: "bts:subscribe",
@@ -217,20 +217,17 @@ func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) e
}
err := b.Websocket.Conn.SendJSONMessage(req)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
b.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (b *Bitstamp) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
req := websocketEventRequest{
Event: "bts:unsubscribe",
@@ -240,15 +237,12 @@ func (b *Bitstamp) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscriptio
}
err := b.Websocket.Conn.SendJSONMessage(req)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
b.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
func (b *Bitstamp) wsUpdateOrderbook(update *websocketOrderBook, p currency.Pair, assetType asset.Item) error {

View File

@@ -253,21 +253,18 @@ func (b *Bittrex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
// Subscribe sends a websocket message to receive data from the channel
func (b *Bittrex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var x int
var errs common.Errors
var errs error
for x = 0; x+wsMessageRateLimit < len(channelsToSubscribe); x += wsMessageRateLimit {
err := b.subscribeSlice(channelsToSubscribe[x : x+wsMessageRateLimit])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
}
err := b.subscribeSlice(channelsToSubscribe[x:])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
if errs != nil {
return errs
}
return nil
return errs
}
func (b *Bittrex) subscribeSlice(channelsToSubscribe []stream.ChannelSubscription) error {
@@ -301,38 +298,32 @@ func (b *Bittrex) subscribeSlice(channelsToSubscribe []stream.ChannelSubscriptio
if err != nil {
return err
}
var errs common.Errors
var errs error
for i := range response.Response {
if !response.Response[i].Success {
errs = append(errs, errors.New("unable to subscribe to "+channels[i]+" - error code "+response.Response[i].ErrorCode))
errs = common.AppendError(errs, errors.New("unable to subscribe to "+channels[i]+" - error code "+response.Response[i].ErrorCode))
continue
}
b.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// Unsubscribe sends a websocket message to receive data from the channel
func (b *Bittrex) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var x int
var errs common.Errors
var errs error
for x = 0; x+wsMessageRateLimit < len(channelsToUnsubscribe); x += wsMessageRateLimit {
err := b.unsubscribeSlice(channelsToUnsubscribe[x : x+wsMessageRateLimit])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
}
err := b.unsubscribeSlice(channelsToUnsubscribe[x:])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
if errs != nil {
return errs
}
return nil
return errs
}
func (b *Bittrex) unsubscribeSlice(channelsToUnsubscribe []stream.ChannelSubscription) error {
@@ -366,18 +357,15 @@ func (b *Bittrex) unsubscribeSlice(channelsToUnsubscribe []stream.ChannelSubscri
if err != nil {
return err
}
var errs common.Errors
var errs error
for i := range response.Response {
if !response.Response[i].Success {
errs = append(errs, errors.New("unable to unsubscribe from "+channels[i]+" - error code "+response.Response[i].ErrorCode))
errs = common.AppendError(errs, errors.New("unable to unsubscribe from "+channels[i]+" - error code "+response.Response[i].ErrorCode))
continue
}
b.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// wsReadData gets and passes on websocket messages for processing

View File

@@ -115,7 +115,7 @@ func (by *Bybit) WsAuth(ctx context.Context) error {
// Subscribe sends a websocket message to receive data from the channel
func (by *Bybit) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
var subReq WsReq
subReq.Topic = channelsToSubscribe[i].Channel
@@ -123,7 +123,7 @@ func (by *Bybit) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
formattedPair, err := by.FormatExchangeCurrency(channelsToSubscribe[i].Currency, asset.Spot)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
if channelsToSubscribe[i].Channel == wsKlines {
@@ -140,20 +140,17 @@ func (by *Bybit) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
}
err = by.Websocket.Conn.SendJSONMessage(subReq)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (by *Bybit) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
var unSub WsReq
@@ -162,7 +159,7 @@ func (by *Bybit) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
formattedPair, err := by.FormatExchangeCurrency(channelsToUnsubscribe[i].Currency, asset.Spot)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
unSub.Parameters = WsParams{
@@ -170,15 +167,12 @@ func (by *Bybit) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
}
err = by.Websocket.Conn.SendJSONMessage(unSub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// wsReadData receives and passes on websocket messages for processing

View File

@@ -121,7 +121,7 @@ func (by *Bybit) WsCoinAuth(ctx context.Context) error {
// SubscribeCoin sends a websocket message to receive data from the channel
func (by *Bybit) SubscribeCoin(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
var sub WsFuturesReq
sub.Topic = subscribe
@@ -129,7 +129,7 @@ func (by *Bybit) SubscribeCoin(channelsToSubscribe []stream.ChannelSubscription)
sub.Args = append(sub.Args, formatArgs(channelsToSubscribe[i].Channel, channelsToSubscribe[i].Params))
err := by.Websocket.Conn.SendJSONMessage(sub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -150,29 +150,25 @@ func formatArgs(channel string, params map[string]interface{}) string {
// UnsubscribeCoin sends a websocket message to stop receiving data from the channel
func (by *Bybit) UnsubscribeCoin(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
var unSub WsFuturesReq
unSub.Topic = unsubscribe
formattedPair, err := by.FormatExchangeCurrency(channelsToUnsubscribe[i].Currency, asset.CoinMarginedFutures)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
unSub.Args = append(unSub.Args, channelsToUnsubscribe[i].Channel+dot+formattedPair.String())
err = by.Websocket.Conn.SendJSONMessage(unSub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// wsCoinReadData gets and passes on websocket messages for processing

View File

@@ -84,7 +84,7 @@ func (by *Bybit) WsFuturesAuth(ctx context.Context) error {
// SubscribeFutures sends a websocket message to receive data from the channel
func (by *Bybit) SubscribeFutures(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
var sub WsFuturesReq
sub.Topic = subscribe
@@ -92,42 +92,35 @@ func (by *Bybit) SubscribeFutures(channelsToSubscribe []stream.ChannelSubscripti
sub.Args = append(sub.Args, formatArgs(channelsToSubscribe[i].Channel, channelsToSubscribe[i].Params))
err := by.Websocket.Conn.SendJSONMessage(sub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// UnsubscribeFutures sends a websocket message to stop receiving data from the channel
func (by *Bybit) UnsubscribeFutures(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
var unSub WsFuturesReq
unSub.Topic = unsubscribe
formattedPair, err := by.FormatExchangeCurrency(channelsToUnsubscribe[i].Currency, asset.Futures)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
unSub.Args = append(unSub.Args, channelsToUnsubscribe[i].Channel+dot+formattedPair.String())
err = by.Websocket.Conn.SendJSONMessage(unSub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
// wsFuturesReadData gets and passes on websocket messages for processing

View File

@@ -86,7 +86,7 @@ func (by *Bybit) WsUSDTAuth(ctx context.Context) error {
// SubscribeUSDT sends a websocket message to receive data from the channel
func (by *Bybit) SubscribeUSDT(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
var sub WsFuturesReq
sub.Topic = subscribe
@@ -94,7 +94,7 @@ func (by *Bybit) SubscribeUSDT(channelsToSubscribe []stream.ChannelSubscription)
sub.Args = append(sub.Args, formatArgs(channelsToSubscribe[i].Channel, channelsToSubscribe[i].Params))
err := by.Websocket.Conn.SendJSONMessage(sub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -107,21 +107,20 @@ func (by *Bybit) SubscribeUSDT(channelsToSubscribe []stream.ChannelSubscription)
// UnsubscribeUSDT sends a websocket message to stop receiving data from the channel
func (by *Bybit) UnsubscribeUSDT(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
var unSub WsFuturesReq
unSub.Topic = unsubscribe
formattedPair, err := by.FormatExchangeCurrency(channelsToUnsubscribe[i].Currency, asset.USDTMarginedFutures)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
unSub.Args = append(unSub.Args, channelsToUnsubscribe[i].Channel+dot+formattedPair.String())
err = by.Websocket.Conn.SendJSONMessage(unSub)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
by.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])

View File

@@ -616,11 +616,11 @@ func (c *COINUT) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
// Subscribe sends a websocket message to receive data from the channel
func (c *COINUT) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
fpair, err := c.FormatExchangeCurrency(channelsToSubscribe[i].Currency, asset.Spot)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
@@ -632,7 +632,7 @@ func (c *COINUT) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
}
err = c.Websocket.Conn.SendJSONMessage(subscribe)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
c.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -645,11 +645,11 @@ func (c *COINUT) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (c *COINUT) Unsubscribe(channelToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelToUnsubscribe {
fpair, err := c.FormatExchangeCurrency(channelToUnsubscribe[i].Currency, asset.Spot)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
@@ -662,32 +662,29 @@ func (c *COINUT) Unsubscribe(channelToUnsubscribe []stream.ChannelSubscription)
resp, err := c.Websocket.Conn.SendMessageReturnResponse(subscribe.Nonce,
subscribe)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
var response map[string]interface{}
err = json.Unmarshal(resp, &response)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
val, ok := response["status"].([]interface{})
if !ok {
errs = append(errs, errors.New("unable to type assert response status"))
errs = common.AppendError(errs, errors.New("unable to type assert response status"))
}
if val[0] != "OK" {
errs = append(errs, fmt.Errorf("%v unsubscribe failed for channel %v",
errs = common.AppendError(errs, fmt.Errorf("%v unsubscribe failed for channel %v",
c.Name,
channelToUnsubscribe[i].Channel))
continue
}
c.Websocket.RemoveSuccessfulUnsubscriptions(channelToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
return errs
}
func (c *COINUT) wsAuthenticate(ctx context.Context) error {

View File

@@ -541,31 +541,28 @@ func (g *Gateio) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
return err
}
var errs common.Errors
var errs error
for k := range payloads {
resp, err := g.Websocket.Conn.SendMessageReturnResponse(payloads[k].ID, payloads[k])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
var response WebsocketAuthenticationResponse
err = json.Unmarshal(resp, &response)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
if response.Result.Status != "success" {
errs = append(errs, fmt.Errorf("%v could not subscribe to %v",
errs = common.AppendError(errs, fmt.Errorf("%v could not subscribe to %v",
g.Name,
payloads[k].Method))
continue
}
g.Websocket.AddSuccessfulSubscriptions(payloads[k].Channels...)
}
if errs != nil {
return errs
}
return nil
return errs
}
func (g *Gateio) generatePayload(channelsToSubscribe []stream.ChannelSubscription) ([]WebsocketRequest, error) {

View File

@@ -498,7 +498,7 @@ func (h *HitBTC) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
// Subscribe sends a websocket message to receive data from the channel
func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
subscribe := WsRequest{
Method: channelsToSubscribe[i].Channel,
@@ -517,7 +517,7 @@ func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
err := h.Websocket.Conn.SendJSONMessage(subscribe)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -530,7 +530,7 @@ func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (h *HitBTC) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
unsubscribeChannel := strings.Replace(channelsToUnsubscribe[i].Channel,
"subscribe",
@@ -552,7 +552,7 @@ func (h *HitBTC) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
err := h.Websocket.Conn.SendJSONMessage(unsubscribe)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])

View File

@@ -553,7 +553,7 @@ func (h *HUOBI) Subscribe(channelsToSubscribe []stream.ChannelSubscription) erro
return err
}
}
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
if (strings.Contains(channelsToSubscribe[i].Channel, "orders.") ||
strings.Contains(channelsToSubscribe[i].Channel, "accounts")) && creds != nil {
@@ -562,7 +562,7 @@ func (h *HUOBI) Subscribe(channelsToSubscribe []stream.ChannelSubscription) erro
wsAccountsOrdersEndPoint+channelsToSubscribe[i].Channel,
channelsToSubscribe[i].Channel)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -572,7 +572,7 @@ func (h *HUOBI) Subscribe(channelsToSubscribe []stream.ChannelSubscription) erro
Subscribe: channelsToSubscribe[i].Channel,
})
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
@@ -593,7 +593,7 @@ func (h *HUOBI) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
return err
}
}
var errs common.Errors
var errs error
for i := range channelsToUnsubscribe {
if (strings.Contains(channelsToUnsubscribe[i].Channel, "orders.") ||
strings.Contains(channelsToUnsubscribe[i].Channel, "accounts")) && creds != nil {
@@ -602,7 +602,7 @@ func (h *HUOBI) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
wsAccountsOrdersEndPoint+channelsToUnsubscribe[i].Channel,
channelsToUnsubscribe[i].Channel)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
@@ -612,7 +612,7 @@ func (h *HUOBI) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
Unsubscribe: channelsToUnsubscribe[i].Channel,
})
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
h.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])

View File

@@ -372,15 +372,15 @@ func (k *Kraken) GetTrades(ctx context.Context, symbol currency.Pair) ([]RecentT
}
if len(data.Error) > 0 {
var errs common.Errors
var errs error
for x := range data.Error {
errString, ok := data.Error[x].(string)
if !ok {
continue
}
errs = append(errs, errors.New(errString))
errs = common.AppendError(errs, errors.New(errString))
}
if len(errs) > 0 {
if errs != nil {
return nil, errs
}
}

View File

@@ -1264,13 +1264,13 @@ channels:
*s = append(*s, outbound)
}
var errs common.Errors
var errs error
for subType, subs := range subscriptions {
for i := range *subs {
if common.StringDataContains(authenticatedChannels, (*subs)[i].Subscription.Name) {
_, err := k.Websocket.AuthConn.SendMessageReturnResponse((*subs)[i].RequestID, (*subs)[i])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
k.Websocket.AddSuccessfulSubscriptions((*subs)[i].Channels...)
@@ -1285,16 +1285,13 @@ channels:
}
_, err := k.Websocket.Conn.SendMessageReturnResponse((*subs)[i].RequestID, (*subs)[i])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
k.Websocket.AddSuccessfulSubscriptions((*subs)[i].Channels...)
}
}
if errs != nil {
return errs
}
return nil
return errs
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
@@ -1339,12 +1336,12 @@ channels:
unsubs = append(unsubs, unsub)
}
var errs common.Errors
var errs error
for i := range unsubs {
if common.StringDataContains(authenticatedChannels, unsubs[i].Subscription.Name) {
_, err := k.Websocket.AuthConn.SendMessageReturnResponse(unsubs[i].RequestID, unsubs[i])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
k.Websocket.RemoveSuccessfulUnsubscriptions(unsubs[i].Channels...)
@@ -1353,15 +1350,12 @@ channels:
_, err := k.Websocket.Conn.SendMessageReturnResponse(unsubs[i].RequestID, unsubs[i])
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
k.Websocket.RemoveSuccessfulUnsubscriptions(unsubs[i].Channels...)
}
if errs != nil {
return errs
}
return nil
return errs
}
// wsAddOrder creates an order, returned order ID if success

View File

@@ -463,9 +463,9 @@ func (ok *Okx) PlaceMultipleOrders(ctx context.Context, args []PlaceOrderRequest
if len(resp) == 0 {
return nil, err
}
var errs common.Errors
var errs error
for x := range resp {
errs = append(errs, fmt.Errorf("error code:%s message: %v", resp[x].SCode, resp[x].SMessage))
errs = common.AppendError(errs, fmt.Errorf("error code:%s message: %v", resp[x].SCode, resp[x].SMessage))
}
return nil, errs
}
@@ -513,10 +513,10 @@ func (ok *Okx) CancelMultipleOrders(ctx context.Context, args []CancelOrderReque
if len(resp) == 0 {
return nil, err
}
errs := common.Errors{}
var errs error
for x := range resp {
if resp[x].SCode != "0" {
errs = append(errs, fmt.Errorf("error code:%s message: %v", resp[x].SCode, resp[x].SMessage))
errs = common.AppendError(errs, fmt.Errorf("error code:%s message: %v", resp[x].SCode, resp[x].SMessage))
}
}
return nil, errs

View File

@@ -1384,10 +1384,10 @@ func (ok *Okx) WsPlaceMultipleOrder(args []PlaceOrderRequestParam) ([]OrderData,
if len(data.Data) == 0 {
return nil, fmt.Errorf("error code:%s message: %v", data.Code, ErrorCodes[data.Code])
}
errs := common.Errors{}
var errs error
for x := range resp.Data {
if resp.Data[x].SCode != "0" {
errs = append(errs, fmt.Errorf("error code:%s message: %s", resp.Data[x].SCode, resp.Data[x].SMessage))
errs = common.AppendError(errs, fmt.Errorf("error code:%s message: %s", resp.Data[x].SCode, resp.Data[x].SMessage))
}
}
return nil, errs
@@ -1514,10 +1514,10 @@ func (ok *Okx) WsCancelMultipleOrder(args []CancelOrderRequestParam) ([]OrderDat
if err != nil {
return nil, err
}
errs := common.Errors{}
var errs error
for x := range resp.Data {
if resp.Data[x].SCode != "0" {
errs = append(errs, fmt.Errorf("error code:%s message: %v", resp.Data[x].SCode, resp.Data[x].SMessage))
errs = common.AppendError(errs, fmt.Errorf("error code:%s message: %v", resp.Data[x].SCode, resp.Data[x].SMessage))
}
}
return nil, errs
@@ -1652,10 +1652,10 @@ func (ok *Okx) WsAmendMultipleOrders(args []AmendOrderRequestParams) ([]OrderDat
if err != nil {
return nil, err
}
errs := common.Errors{}
var errs error
for x := range resp.Data {
if resp.Data[x].SCode != "0" {
errs = append(errs, fmt.Errorf("error code:%s message: %v", resp.Data[x].SCode, resp.Data[x].SMessage))
errs = common.AppendError(errs, fmt.Errorf("error code:%s message: %v", resp.Data[x].SCode, resp.Data[x].SMessage))
}
}
return nil, errs

View File

@@ -1109,18 +1109,14 @@ func (c *Cancel) Validate(opt ...validate.Checker) error {
return ErrCancelOrderIsNil
}
var errs common.Errors
var errs error
for _, o := range opt {
err := o.Check()
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
}
if errs != nil {
return errs
}
return nil
return errs
}
// Validate checks internal struct requirements and returns filter requirement
@@ -1142,17 +1138,14 @@ func (g *GetOrdersRequest) Validate(opt ...validate.Checker) error {
return errUnrecognisedOrderType
}
var errs common.Errors
var errs error
for _, o := range opt {
err := o.Check()
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
}
if errs != nil {
return errs
}
return nil
return errs
}
// Filter reduces slice by optional fields
@@ -1183,14 +1176,13 @@ func (m *Modify) Validate(opt ...validate.Checker) error {
return ErrAssetNotSet
}
var errs common.Errors
var errs error
for _, o := range opt {
err := o.Check()
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
}
}
if errs != nil {
return errs
}

View File

@@ -549,7 +549,7 @@ func (p *Poloniex) Subscribe(sub []stream.ChannelSubscription) error {
return err
}
}
var errs common.Errors
var errs error
channels:
for i := range sub {
subscriptionRequest := WsCommand{
@@ -560,7 +560,7 @@ channels:
sub[i].Channel) && creds != nil:
err := p.wsSendAuthorisedCommand(creds.Secret, creds.Key, "subscribe")
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue channels
}
p.Websocket.AddSuccessfulSubscriptions(sub[i])
@@ -574,7 +574,7 @@ channels:
err := p.Websocket.Conn.SendJSONMessage(subscriptionRequest)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
@@ -596,7 +596,7 @@ func (p *Poloniex) Unsubscribe(unsub []stream.ChannelSubscription) error {
return err
}
}
var errs common.Errors
var errs error
channels:
for i := range unsub {
unsubscriptionRequest := WsCommand{
@@ -607,7 +607,7 @@ channels:
unsub[i].Channel) && creds != nil:
err := p.wsSendAuthorisedCommand(creds.Secret, creds.Key, "unsubscribe")
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue channels
}
p.Websocket.RemoveSuccessfulUnsubscriptions(unsub[i])
@@ -620,7 +620,7 @@ channels:
}
err := p.Websocket.Conn.SendJSONMessage(unsubscriptionRequest)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
p.Websocket.RemoveSuccessfulUnsubscriptions(unsub[i])

View File

@@ -65,7 +65,6 @@ func AddTradesToBuffer(exchangeName string, data ...Data) error {
if len(data) == 0 {
return nil
}
var errs common.Errors
if atomic.AddInt32(&processor.started, 0) == 0 {
var wg sync.WaitGroup
wg.Add(1)
@@ -73,13 +72,14 @@ func AddTradesToBuffer(exchangeName string, data ...Data) error {
wg.Wait()
}
validDatas := make([]Data, 0, len(data))
var errs error
for i := range data {
if data[i].Price == 0 ||
data[i].Amount == 0 ||
data[i].CurrencyPair.IsEmpty() ||
data[i].Exchange == "" ||
data[i].Timestamp.IsZero() {
errs = append(errs, fmt.Errorf("%v received invalid trade data: %+v", exchangeName, data[i]))
errs = common.AppendError(errs, fmt.Errorf("%v received invalid trade data: %+v", exchangeName, data[i]))
continue
}
@@ -99,7 +99,7 @@ func AddTradesToBuffer(exchangeName string, data ...Data) error {
}
uu, err := uuid.NewV4()
if err != nil {
errs = append(errs, fmt.Errorf("%s uuid failed to generate for trade: %+v", exchangeName, data[i]))
errs = common.AppendError(errs, fmt.Errorf("%s uuid failed to generate for trade: %+v", exchangeName, data[i]))
}
data[i].ID = uu
validDatas = append(validDatas, data[i])
@@ -107,10 +107,7 @@ func AddTradesToBuffer(exchangeName string, data ...Data) error {
processor.mutex.Lock()
processor.buffer = append(processor.buffer, validDatas...)
processor.mutex.Unlock()
if len(errs) > 0 {
return errs
}
return nil
return errs
}
// Run will save trade data to the database in batches

View File

@@ -299,7 +299,7 @@ func (z *ZB) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error
// Subscribe sends a websocket message to receive data from the channel
func (z *ZB) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
var errs error
for i := range channelsToSubscribe {
subscriptionRequest := Subscription{
Event: zWebsocketAddChannel,
@@ -307,7 +307,7 @@ func (z *ZB) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
}
err := z.Websocket.Conn.SendJSONMessage(subscriptionRequest)
if err != nil {
errs = append(errs, err)
errs = common.AppendError(errs, err)
continue
}
z.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])