Skip to content

Commit

Permalink
Parse Binance API error response
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzen committed Sep 14, 2024
1 parent c3ed943 commit 4adf0f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 25 additions & 3 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
Expand All @@ -27,7 +28,6 @@ import (
"decred.org/dcrdex/client/mm/libxc/bntypes"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/calc"
"decred.org/dcrdex/dex/dexnet"
"decred.org/dcrdex/dex/encode"
"decred.org/dcrdex/dex/utils"
)
Expand Down Expand Up @@ -1931,6 +1931,28 @@ func binanceMarketToDexMarkets(binanceBaseSymbol, binanceQuoteSymbol string, tok
}

func requestInto(req *http.Request, thing interface{}) error {
// bnc.log.Tracef("Sending request: %+v", req)
return dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24))
var sizeLimit int64 = 1 << 24

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("error performing request: %w", err)
}
defer resp.Body.Close()
// https://binance-docs.github.io/apidocs/websocket_api/en/#response-format
if resp.StatusCode != http.StatusOK {
var apiResp bntypes.ApiResponse
reader := io.LimitReader(resp.Body, sizeLimit)
if err = json.NewDecoder(reader).Decode(&apiResp); err != nil {
return fmt.Errorf("error decoding response: %w", err)
}
return fmt.Errorf("API error %d: %s (%d)", resp.StatusCode, apiResp.Msg, apiResp.Code)
}
if thing == nil {
return nil
}
reader := io.LimitReader(resp.Body, sizeLimit)
if err = json.NewDecoder(reader).Decode(thing); err != nil {
return fmt.Errorf("error decoding request: %w", err)
}
return nil
}
5 changes: 5 additions & 0 deletions client/mm/libxc/bntypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,8 @@ type MarketTicker24 struct {
LastId int64 `json:"lastId"`
Count int64 `json:"count"`
}

type ApiResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

0 comments on commit 4adf0f7

Please sign in to comment.