Skip to content

Commit

Permalink
Add GetErrorMessageAsync() extensions for exception error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Dec 1, 2018
1 parent 7e64028 commit c01aaf1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v2.0.2
* Added GetErrorMessageAsync() extension method.

## v2.0.1
* Production ready. Models and APIs finalized.

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ The `order` object returned by the trading engine will have similar values to th
* [`CoinbaseProWebSocket`](https://docs.pro.coinbase.com/?r=1#websocket-feed) - [Examples](https://github.com/bchavez/Coinbase.Pro/blob/master/Source/Coinbase.Tests/IntegrationTests/WebSocketTests.cs)


### Error Handling
When errors occur after calling an API, **Coinbase Pro** delivers error messages in the response body of an failed HTTP call. To get the error message of a failed API call, first wrap your call in a `try/catch` statement and handle the `Exception ex`. The `GetErrorMessageAsync()` extension method will read the response body of the failed HTTP call as shown below:
```csharp
try
{
var order = await client.Orders.PlaceLimitOrderAsync(
OrderSide.Buy, "BTCX-USDX", size: 1, limitPrice: 5000m);
}
catch (Exception ex)
{
var errorMsg = await ex.GetErrorMessageAsync();
Console.WriteLine(errorMsg);
}
//OUTPUT: "Product not found"
```

### Pagination
Some **Coinbase Pro** APIs are [paginable](https://docs.pro.coinbase.com/?r=1#pagination). However, **Coinbase Pro**'s paging can be [a little confusing](https://docs.pro.coinbase.com/?r=1#pagination) at first. So, let's review. Consider the following diagram below that illustrates the `Current Point In Time` over a paginable set of data with an item size of 5 items per page:
```
Expand Down
26 changes: 26 additions & 0 deletions Source/Coinbase.Pro/CoinbaseProClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Coinbase.Pro.Models;
using Flurl.Http;
using Flurl.Http.Configuration;

Expand Down Expand Up @@ -93,4 +95,28 @@ async Task SetHeaders(HttpCall http)
settings.BeforeCallAsync = SetHeaders;
}
}

public static class ExtensionsForExceptions
{
/// <summary>
/// Parses the response body of the failed HTTP call return any error status messages.
/// </summary>
public static async Task<string> GetErrorMessageAsync(this FlurlHttpException ex)
{
if( ex is null ) return null;

var error = await ex.GetResponseJsonAsync<JsonResponse>()
.ConfigureAwait(false);

return error?.Message;
}

/// <summary>
/// Parses the response body of the failed HTTP call return any error status messages.
/// </summary>
public static Task<string> GetErrorMessageAsync(this Exception ex)
{
return GetErrorMessageAsync(ex as FlurlHttpException);
}
}
}
18 changes: 18 additions & 0 deletions Source/Coinbase.Tests/IntegrationTests/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ public async Task create_order4()
o.Dump();
}

[Test]
public async Task create_order5_with_error()
{
try
{
var o = await this.client.Orders.PlaceLimitOrderAsync(
OrderSide.Buy, "BTCX-USD", size: 1, limitPrice: 5000m);

//o.Dump();
}
catch (Exception ex)
{
var errorMsg = await ex.GetErrorMessageAsync();
errorMsg.Dump();
}

}

[Test]
public async Task get_fills1()
{
Expand Down

0 comments on commit c01aaf1

Please sign in to comment.