| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Net.Sockets; |
| | | 4 | | |
| | | 5 | | namespace IceRpc.Transports; |
| | | 6 | | |
| | | 7 | | /// <summary>Provides an extension method for <see cref="SocketException"/> to convert it into an <see |
| | | 8 | | /// cref="IceRpcException"/>.</summary> |
| | | 9 | | public 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) |
| | 75 | 17 | | { |
| | 75 | 18 | | innerException ??= exception; |
| | 75 | 19 | | IceRpcError errorCode = exception.SocketErrorCode switch |
| | 75 | 20 | | { |
| | 6 | 21 | | SocketError.AddressAlreadyInUse => IceRpcError.AddressInUse, |
| | 0 | 22 | | SocketError.ConnectionAborted => IceRpcError.ConnectionAborted, |
| | 75 | 23 | | // Shutdown matches EPIPE and ConnectionReset matches ECONNRESET. Both are the result of the peer closing |
| | 75 | 24 | | // non-gracefully the connection. EPIPE is returned if the socket is closed and the send buffer is empty |
| | 75 | 25 | | // while ECONNRESET is returned if the send buffer is not empty. |
| | 57 | 26 | | SocketError.ConnectionReset => IceRpcError.ConnectionAborted, |
| | 2 | 27 | | SocketError.HostUnreachable => IceRpcError.ServerUnreachable, |
| | 0 | 28 | | SocketError.NetworkUnreachable => IceRpcError.ServerUnreachable, |
| | 4 | 29 | | SocketError.Shutdown => IceRpcError.ConnectionAborted, |
| | 4 | 30 | | SocketError.ConnectionRefused => IceRpcError.ConnectionRefused, |
| | 0 | 31 | | SocketError.OperationAborted => IceRpcError.OperationAborted, |
| | 2 | 32 | | _ => IceRpcError.IceRpcError |
| | 75 | 33 | | }; |
| | | 34 | | |
| | 75 | 35 | | return new IceRpcException(errorCode, innerException); |
| | 75 | 36 | | } |
| | | 37 | | } |