-
Notifications
You must be signed in to change notification settings - Fork 75
Exception Handling
sonvister edited this page Mar 14, 2018
·
7 revisions
This code demonstrates how to handle exceptions from the IBinanceApi
methods.
NOTE: Handling exceptions with this level of precision is only applicable to processes that an application may retry should the first attempt fail (e.g. order placement or withdraw requests).
try
{
var order = await api.PlaceAsync(user, new MarketOrder()
{
Symbol = Symbol.BTC_USDT,
Side = OrderSide.Sell,
Quantity = 0.1m
});
}
catch (OperationCanceledException) // and TaskCanceledException
{
if (!token.IsCancellationRequested) // or token is not provided.
{
// HttpClient timeout (handle same as HTTP 504 response below).
}
// Otherwise, canceled by user (ignore).
}
catch (BinanceUnknownStatusException) // ...is BinanceHttpException (w/ status code 504)
{
// HTTP 504 return code is used when the API successfully sent the message but not get a response within the timeout period.
// It is important to NOT treat this as a failure; the execution status is UNKNOWN and could have been a success.
}
catch (BinanceHttpException e) // ...is BinanceApiException
{
// The request failed; handle exception based on HTTP status code.
if (e.IsClientError())
{
// HTTP 4XX return codes are used for for malformed requests; the issue is on the sender's side.
}
else if (e.IsServerError())
{
// HTTP 5XX return codes are used for internal errors; the issue is on Binance's side.
}
else
{
// Other HTTP return codes...
}
}
catch (BinanceApiException)
{
// Respond to other Binance API exceptions (typically JSON deserialization failures).
}
catch (Exception)
{
// Other exceptions...
}
- Verify connection to the Binance server (minimal examples).
- Get the market depth (order book) for a symbol.
- Maintain a real-time order book cache for a symbol.
- Get the aggregate trades for a symbol.
- Maintain a real-time trade history cache for a symbol.
- Get the candlesticks for a symbol.
- Maintain a real-time price chart cache for a symbol.
- Get the 24-hour statistics for a symbol.
- Get current prices for all symbols for a price ticker.
- Get best price and quantity on the order book for all symbols.
- Get a list of all current symbols.
- Place a LIMIT order.
- Place a MARKET order.
- Place a TEST order to verify client order properties.
- Look-up an existing order to check status.
- Cancel an open order.
- Get all open orders for a symbol.
- Get all orders for a symbol.
- Get current account information.
- Get account trades for a symbol.
- Submit a withdraw request.
- Get deposit history.
- Get withdraw history.
- Donate BTC to the creator of this library.