< Summary

Information
Class: NostrSure.Infrastructure.Client.Implementation.ConnectionErrorHandler
Assembly: NostrSure.Infrastructure
File(s): /home/runner/work/NostrSure/NostrSure/NostrSure.Infrastructure/Client/Implementation/ConnectionErrorHandler.cs
Line coverage
95%
Covered lines: 42
Uncovered lines: 2
Coverable lines: 44
Total lines: 69
Line coverage: 95.4%
Branch coverage
86%
Covered branches: 25
Total branches: 29
Branch coverage: 86.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleErrorAsync()50%44100%
ShouldReconnect(...)92%252594.28%

File(s)

/home/runner/work/NostrSure/NostrSure/NostrSure.Infrastructure/Client/Implementation/ConnectionErrorHandler.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using NostrSure.Infrastructure.Client.Abstractions;
 3using System.Net.Sockets;
 4using System.Net.WebSockets;
 5
 6namespace NostrSure.Infrastructure.Client.Implementation;
 7
 8/// <summary>
 9/// Centralized error handling for WebSocket connections
 10/// </summary>
 11public sealed class ConnectionErrorHandler : IConnectionErrorHandler
 12{
 13    private readonly ILogger<ConnectionErrorHandler>? _logger;
 14
 815    public ConnectionErrorHandler(ILogger<ConnectionErrorHandler>? logger = null)
 816    {
 817        _logger = logger;
 818    }
 19
 20    public event EventHandler<Exception>? ErrorOccurred;
 21
 22    public async Task HandleErrorAsync(Exception exception, string context)
 123    {
 124        _logger?.LogError(exception, "WebSocket error in {Context}: {Message}", context, exception.Message);
 25
 26        // Fire error event
 127        ErrorOccurred?.Invoke(this, exception);
 28
 29        // Allow for async error handling if needed
 130        await Task.CompletedTask;
 131    }
 32
 33    public bool ShouldReconnect(Exception exception)
 1634    {
 1635        return exception switch
 1636        {
 1637            // Network-related exceptions that warrant retry
 138            HttpRequestException => true,
 139            SocketException => true,
 140            TimeoutException => true,
 1641
 1642            // WebSocket-specific exceptions
 943            WebSocketException wsEx => wsEx.WebSocketErrorCode switch
 944            {
 145                WebSocketError.ConnectionClosedPrematurely => true,
 046                WebSocketError.Faulted => true,
 147                WebSocketError.HeaderError => false, // Don't retry on header errors
 148                WebSocketError.InvalidMessageType => false, // Don't retry on invalid message types
 149                WebSocketError.InvalidState => false, // Don't retry on invalid state
 150                WebSocketError.NativeError => true,
 151                WebSocketError.NotAWebSocket => false, // Don't retry if not a WebSocket
 152                WebSocketError.Success => false, // This shouldn't be an error
 153                WebSocketError.UnsupportedProtocol => false, // Don't retry on protocol mismatch
 154                WebSocketError.UnsupportedVersion => false, // Don't retry on version mismatch
 055                _ => true // Default to retry for unknown WebSocket errors
 956            },
 1657
 1658            // Operation cancelled - usually intentional, don't retry
 159            OperationCanceledException => false,
 1660
 1661            // Invalid operations - usually programming errors, don't retry
 162            InvalidOperationException => false,
 163            ArgumentException => false,
 1664
 1665            // Default to retry for unknown exceptions
 166            _ => true
 1667        };
 1668    }
 69}