< Summary

Information
Class: IceRpc.Transports.SocketExceptionExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/SocketExceptionExtensions.cs
Tag: 1856_27024993493
Line coverage
82%
Covered lines: 55
Uncovered lines: 12
Coverable lines: 67
Total lines: 85
Line coverage: 82%
Branch coverage
25%
Covered branches: 7
Total branches: 28
Branch coverage: 25%
Method coverage
100%
Covered methods: 1
Fully covered methods: 0
Total methods: 1
Method coverage: 100%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToIceRpcException(...)25%342880.59%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/SocketExceptionExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Net.Sockets;
 4
 5namespace IceRpc.Transports;
 6
 7/// <summary>Provides an extension method for <see cref="SocketException"/> to convert it into an <see
 8/// cref="IceRpcException"/>.</summary>
 9public static class SocketExceptionExtensions
 10{
 11    /// <summary>Converts a <see cref="SocketException"/> into an <see cref="IceRpcException" />.</summary>
 12    /// <param name="exception">The exception to convert.</param>
 13    /// <param name="innerException">The inner exception for the <see cref="IceRpcException"/>, when
 14    /// <see langword="null"/> <paramref name="exception"/> is used as the inner exception.</param>
 15    /// <returns>The <see cref="IceRpcException"/> created from the <see cref="SocketException"/>.</returns>
 16    public static IceRpcException ToIceRpcException(this SocketException exception, Exception? innerException = null)
 4317    {
 4318        innerException ??= exception;
 4319        IceRpcError errorCode = exception.SocketErrorCode switch
 4320        {
 4321            // Address is already in use when attempting to bind a listening socket.
 322            SocketError.AddressAlreadyInUse => IceRpcError.AddressInUse,
 4323
 4324            // ConnectionAborted is reported by the OS and is often triggered by the peer or by a network failure.
 025            SocketError.ConnectionAborted => IceRpcError.ConnectionAborted,
 4326
 4327            // Shutdown matches EPIPE and ConnectionReset matches ECONNRESET. Both are the result of the peer
 4328            // closing the connection non-gracefully.
 4329            //
 4330            // EPIPE is returned when writing to a socket that has been closed and the send buffer is empty.
 4331            // ECONNRESET is returned when the connection is reset while data is still pending in the send buffer.
 4332            //
 4333            // In both cases the connection can no longer be used.
 3534            SocketError.ConnectionReset => IceRpcError.ConnectionAborted,
 235            SocketError.Shutdown => IceRpcError.ConnectionAborted,
 4336
 4337            // NetworkReset indicates that an established connection became invalid due to a network event
 4338            // (for example interface reset, Wi-Fi reconnect, VPN reconnect).
 039            SocketError.NetworkReset => IceRpcError.ConnectionAborted,
 4340
 4341            // The server is reachable but no process is listening on the target port.
 242            SocketError.ConnectionRefused => IceRpcError.ConnectionRefused,
 4343
 4344            // These errors indicate the remote server cannot be reached. This includes routing failures,
 4345            // local network failures, OS-level TCP timeouts, and host reachability failures.
 4346            //
 4347            // The TimedOut error refers to the OS TCP stack exhausting SYN retransmissions when
 4348            // attempting to connect — not to IceRPC-level timeouts, which result in
 4349            // OperationCanceledException via CancellationToken.
 4350            //
 4351            // With the async socket API used by IceRPC, these errors occur during connection
 4352            // establishment. On an established TCP connection, the kernel absorbs ICMP errors
 4353            // and retransmits internally; the application sees ConnectionReset (mapped to
 4354            // ConnectionAborted above) rather than these reachability errors.
 4355            //
 4356            // They are grouped as ServerUnreachable because from the RPC perspective the connection
 4357            // cannot be established.
 158            SocketError.HostUnreachable => IceRpcError.ServerUnreachable,
 059            SocketError.NetworkUnreachable => IceRpcError.ServerUnreachable,
 060            SocketError.NetworkDown => IceRpcError.ServerUnreachable,
 061            SocketError.HostDown => IceRpcError.ServerUnreachable,
 062            SocketError.TimedOut => IceRpcError.ServerUnreachable,
 4363
 4364            // Name resolution failures are also mapped to ServerUnreachable so that the connection cache
 4365            // can treat DNS failures and transport reachability failures uniformly and try alternate
 4366            // server addresses.
 4367            //
 4368            // These errors always occur before connection establishment (during name resolution).
 4369            //
 4370            // Some of these errors (for example HostNotFound) are often permanent configuration issues,
 4371            // but IceRPC keeps the public error model small and leaves retry decisions to higher layers.
 072            SocketError.HostNotFound => IceRpcError.ServerUnreachable,
 073            SocketError.TryAgain => IceRpcError.ServerUnreachable,
 074            SocketError.NoRecovery => IceRpcError.ServerUnreachable,
 075            SocketError.NoData => IceRpcError.ServerUnreachable,
 4376
 4377            // Operation was aborted locally, typically due to cancellation or socket disposal.
 078            SocketError.OperationAborted => IceRpcError.OperationAborted,
 4379
 080            _ => IceRpcError.IceRpcError
 4381        };
 82
 4383        return new IceRpcException(errorCode, innerException);
 4384    }
 85}