mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-12 23:36:55 +08:00
robustness: add HTTP status code checks to market data & provider APIs
- market/api_client.go: check StatusCode for exchangeInfo, klines, price endpoints + add truncateBody helper for safe error messages - market/data.go: check StatusCode for openInterest and premiumIndex - provider/coinank: check StatusCode for GET and POST requests - provider/twelvedata: check StatusCode for time series and quote APIs - Prevents confusing JSON unmarshal errors when APIs return 429/500/etc
This commit is contained in:
@@ -71,6 +71,9 @@ func get(ctx context.Context, path string, paramsMap map[string]string) (string,
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("CoinAnk API error (status %d): %.512s", resp.StatusCode, string(body))
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,9 @@ func (c *CoinankClient) Get(ctx context.Context, path string, paramsMap map[stri
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("CoinAnk GET error (status %d): %.512s", resp.StatusCode, string(body))
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
@@ -88,6 +91,9 @@ func (c *CoinankClient) Post(ctx context.Context, path string, data any) (string
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("CoinAnk POST error (status %d): %.512s", resp.StatusCode, string(body))
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -129,6 +129,9 @@ func (c *Client) GetTimeSeries(ctx context.Context, symbol string, interval stri
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("TwelveData API error (status %d): %.512s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Parse response
|
||||
var result TimeSeriesResponse
|
||||
@@ -176,6 +179,9 @@ func (c *Client) GetQuote(ctx context.Context, symbol string) (*QuoteResponse, e
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("TwelveData API error (status %d): %.512s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Parse response
|
||||
var result QuoteResponse
|
||||
|
||||
Reference in New Issue
Block a user