Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Binance API error response #2912

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 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 @@ -1915,7 +1915,34 @@ func binanceMarketToDexMarkets(binanceBaseSymbol, binanceQuoteSymbol string, tok
return markets
}

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

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 BNApiResponse
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)
}
Comment on lines -1920 to +1939
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add this functionality to dexnet. 50005f4...buck54321:dexnet-errparse

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
}
Loading