diff --git a/Alpaca.Markets.Extensions/Reconnection/ClientWithReconnectBase.cs b/Alpaca.Markets.Extensions/Reconnection/ClientWithReconnectBase.cs index 73188f03a..5bb001c3d 100644 --- a/Alpaca.Markets.Extensions/Reconnection/ClientWithReconnectBase.cs +++ b/Alpaca.Markets.Extensions/Reconnection/ClientWithReconnectBase.cs @@ -92,8 +92,21 @@ protected virtual ValueTask OnReconnection( CancellationToken cancellationToken) => new(); // DO nothing by default for auto-resubscribed clients. - private async void handleSocketClosed() => - await handleSocketClosedAsync().ConfigureAwait(false); + + [SuppressMessage( + "Design", "CA1031:Do not catch general exception types", + Justification = "Expected behavior - we report exceptions via OnError event.")] + private async void handleSocketClosed() + { + try + { + await handleSocketClosedAsync().ConfigureAwait(false); + } + catch (Exception exception) + { + handleOnError(exception); + } + } [SuppressMessage( "Design", "CA1031:Do not catch general exception types", @@ -174,6 +187,7 @@ await OnReconnection(_cancellationTokenSource.Token) [SuppressMessage( "Design", "CA1031:Do not catch general exception types", Justification = "Expected behavior - we report exceptions via OnError event.")] + // ReSharper disable once AsyncVoidMethod private async void handleOnError( Exception exception) { diff --git a/Alpaca.Markets.Extensions/Subscriptions/DisposableAlpacaDataSubscription.cs b/Alpaca.Markets.Extensions/Subscriptions/DisposableAlpacaDataSubscription.cs index 3b6f39f28..1cb74a17d 100644 --- a/Alpaca.Markets.Extensions/Subscriptions/DisposableAlpacaDataSubscription.cs +++ b/Alpaca.Markets.Extensions/Subscriptions/DisposableAlpacaDataSubscription.cs @@ -1,4 +1,6 @@ -namespace Alpaca.Markets.Extensions; +using System.Diagnostics; + +namespace Alpaca.Markets.Extensions; internal sealed class DisposableAlpacaDataSubscription : IDisposableAlpacaDataSubscription @@ -39,8 +41,20 @@ public event Action? Received remove => _subscription.Received -= value; } - public async void Dispose() => - await DisposeAsync().ConfigureAwait(false); + [SuppressMessage( + "Design", "CA1031:Do not catch general exception types", + Justification = "Expected behavior - we report exceptions via OnError event.")] + public async void Dispose() + { + try + { + await DisposeAsync().ConfigureAwait(false); + } + catch (Exception exception) + { + Trace.TraceInformation(exception.Message); + } + } public async ValueTask DisposeAsync() { diff --git a/Alpaca.Markets/AlpacaStreamingClient.cs b/Alpaca.Markets/AlpacaStreamingClient.cs index 1828749ce..198c2755d 100644 --- a/Alpaca.Markets/AlpacaStreamingClient.cs +++ b/Alpaca.Markets/AlpacaStreamingClient.cs @@ -31,16 +31,27 @@ internal AlpacaStreamingClient( /// public event Action? OnTradeUpdate; + + [SuppressMessage( + "Design", "CA1031:Do not catch general exception types", + Justification = "Expected behavior - we report exceptions via OnError event.")] protected override async void OnOpened() { - await SendAsJsonStringAsync(new JsonAuthRequest + try { - Action = JsonAction.Authenticate, - Data = Configuration.SecurityId - .GetAuthenticationData() - }).ConfigureAwait(false); + await SendAsJsonStringAsync(new JsonAuthRequest + { + Action = JsonAction.Authenticate, + Data = Configuration.SecurityId + .GetAuthenticationData() + }).ConfigureAwait(false); - base.OnOpened(); + base.OnOpened(); + } + catch (Exception exception) + { + HandleError(exception); + } } [SuppressMessage( diff --git a/global.json b/global.json new file mode 100644 index 000000000..7e8c02767 --- /dev/null +++ b/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "8.0.404" + } +} \ No newline at end of file