Skip to content

Commit cbdd172

Browse files
authored
standardize exception types (#735)
- use NotImplementedException, InvalidOperationException, NotSupportedException, ArgumentException, ArgumentNullException when appropriate - opens up room in the future to create more specific exceptions derived from above exceptions
1 parent ddc7138 commit cbdd172

24 files changed

+53
-54
lines changed

src/ExchangeSharp/API/Common/BaseAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public async Task<object> GenerateNonceAsync()
418418
break;
419419

420420
default:
421-
throw new InvalidOperationException("Invalid nonce style: " + NonceStyle);
421+
throw new NotImplementedException("Invalid nonce style: " + NonceStyle);
422422
}
423423

424424
// check for duplicate nonce

src/ExchangeSharp/API/Exchanges/Aquanow/ExchangeAquanowAPI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dicti
105105
// DONE
106106
protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
107107
{
108-
if (order.IsPostOnly != null) throw new NotImplementedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
108+
if (order.IsPostOnly != null) throw new NotSupportedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
109109
// In Aquanow market order, when buying crypto the amount of crypto that is bought is the receiveQuantity
110110
// and when selling the amount of crypto that is sold is the deliverQuantity
111111
string amountParameter = order.IsBuy ? "receiveQuantity" : "deliverQuantity";
@@ -201,7 +201,7 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
201201
{
202202
return null;
203203
}
204-
if (isClientOrderId) throw new NotImplementedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
204+
if (isClientOrderId) throw new NotSupportedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
205205
var payload = await GetNoncePayloadAsync();
206206
JToken result = await MakeJsonRequestAsync<JToken>($"/trades/v1/order?orderId={orderId}", null, payload, "GET");
207207
bool isBuy = result["tradeSide"].ToStringInvariant() == "buy" ? true : false;

src/ExchangeSharp/API/Exchanges/BL3P/ExchangeBL3PAPI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private string GetSignKey(IHttpWebRequest request, string formData)
233233

234234
protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
235235
{
236-
if (order.IsPostOnly != null) throw new NotImplementedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
236+
if (order.IsPostOnly != null) throw new NotSupportedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
237237
var roundedAmount = order.RoundAmount();
238238
var amountInt = converterToEight.FromDecimal(roundedAmount);
239239

@@ -314,7 +314,7 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
314314
{
315315
if (string.IsNullOrWhiteSpace(marketSymbol))
316316
throw new ArgumentException("Value cannot be null or whitespace.", nameof(marketSymbol));
317-
if (isClientOrderId) throw new NotImplementedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
317+
if (isClientOrderId) throw new NotSupportedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
318318

319319
var data = new Dictionary<string, object>
320320
{

src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
554554
else if (order.IsPostOnly == true)
555555
{
556556
if (order.OrderType == OrderType.Limit) payload["type"] = "LIMIT_MAKER"; // LIMIT_MAKER are LIMIT orders that will be rejected if they would immediately match and trade as a taker.
557-
else throw new NotImplementedException("PostOnly with non limit orders are not currently supported on Binance. Please submit a PR if you are interested in this feature");
557+
else throw new NotSupportedException("PostOnly with non limit orders are not currently supported on Binance. Please submit a PR if you are interested in this feature");
558558
}
559559
else
560560
payload["type"] = order.OrderType.ToStringUpperInvariant();
@@ -587,7 +587,7 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
587587
Dictionary<string, object> payload = await GetNoncePayloadAsync();
588588
if (string.IsNullOrWhiteSpace(marketSymbol))
589589
{
590-
throw new InvalidOperationException("Binance single order details request requires symbol");
590+
throw new ArgumentNullException("Binance single order details request requires symbol");
591591
}
592592
payload["symbol"] = marketSymbol!;
593593

src/ExchangeSharp/API/Exchanges/BitBank/ExchangeBitBankAPI.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected override Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrad
128128
protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
129129
{
130130
if (order.OrderType == OrderType.Stop)
131-
throw new InvalidOperationException("Bitbank does not support stop order");
131+
throw new NotSupportedException("Bitbank does not support stop order");
132132
Dictionary<string, object> payload = await GetNoncePayloadAsync();
133133
if (order.IsPostOnly != null)
134134
payload["post_only"] = order.IsPostOnly;
@@ -154,11 +154,11 @@ protected override async Task OnCancelOrderAsync(string orderId, string marketSy
154154

155155
protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
156156
{
157-
if (isClientOrderId) throw new NotImplementedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
157+
if (isClientOrderId) throw new NotSupportedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
158158
var payload = await GetNoncePayloadAsync();
159159
payload.Add("order_id", orderId);
160-
if (marketSymbol == null)
161-
throw new InvalidOperationException($"BitBank API requires marketSymbol for {nameof(GetOrderDetailsAsync)}");
160+
if (string.IsNullOrWhiteSpace(marketSymbol))
161+
throw new ArgumentNullException($"BitBank API requires marketSymbol for {nameof(GetOrderDetailsAsync)}");
162162
payload.Add("pair", marketSymbol);
163163
JToken token = await MakeJsonRequestAsync<JToken>("/user/spot/order", baseUrl: BaseUrlPrivate, payload: payload);
164164
return ParseOrder(token);

src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
504504

505505
protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
506506
{
507-
if (isClientOrderId) throw new NotImplementedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
507+
if (isClientOrderId) throw new NotSupportedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
508508
if (string.IsNullOrWhiteSpace(orderId))
509509
{
510510
return null;

src/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private static Dictionary<string, decimal> ExtractDictionary(JObject responseObj
195195

196196
protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
197197
{
198-
if (order.IsPostOnly != null) throw new NotImplementedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
198+
if (order.IsPostOnly != null) throw new NotSupportedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
199199
string action = order.IsBuy ? "buy" : "sell";
200200
string market = order.OrderType == OrderType.Market ? "/market" : "";
201201
string url = $"/{action}{market}/{order.MarketSymbol}/";

src/ExchangeSharp/API/Exchanges/Bittrex/ExchangeBittrexAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
415415

416416
protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
417417
{
418-
if (isClientOrderId) throw new NotImplementedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
418+
if (isClientOrderId) throw new NotSupportedException("Querying by client order ID is not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature");
419419
if (string.IsNullOrWhiteSpace(orderId))
420420
{
421421
return null;

src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
819819

820820
public async Task<ExchangeOrderResult> OnAmendOrderAsync(ExchangeOrderRequest order)
821821
{
822-
if (order.IsPostOnly != null) throw new NotImplementedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
822+
if (order.IsPostOnly != null) throw new NotSupportedException("Post Only orders are not supported by this exchange or not implemented in ExchangeSharp. Please submit a PR if you are interested in this feature.");
823823
var payload = new Dictionary<string, object>();
824824
payload["symbol"] = order.MarketSymbol;
825825
if(order.OrderId != null)

src/ExchangeSharp/API/Exchanges/Coinbase/ExchangeCoinbaseAPI.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,10 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
741741
{ // Orders may be queried using either the exchange assigned id or the client assigned client_oid. When using client_oid it must be preceded by the client: namespace.
742742
JToken obj = await MakeJsonRequestAsync<JToken>("/orders/" + (isClientOrderId ? "client:" : "") + orderId,
743743
null, await GetNoncePayloadAsync(), "GET");
744-
return ParseOrder(obj);
744+
var order = ParseOrder(obj);
745+
if (!order.MarketSymbol.Equals(marketSymbol, StringComparison.InvariantCultureIgnoreCase))
746+
throw new DataMisalignedException($"Order {orderId} found, but symbols {order.MarketSymbol} and {marketSymbol} don't match");
747+
else return order;
745748
}
746749

747750
protected override async Task<IEnumerable<ExchangeOrderResult>> OnGetOpenOrderDetailsAsync(string marketSymbol = null)

0 commit comments

Comments
 (0)