| | 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) |
| 70 | 17 | | { |
| 70 | 18 | | innerException ??= exception; |
| 70 | 19 | | IceRpcError errorCode = exception.SocketErrorCode switch |
| 70 | 20 | | { |
| 6 | 21 | | SocketError.AddressAlreadyInUse => IceRpcError.AddressInUse, |
| 0 | 22 | | SocketError.ConnectionAborted => IceRpcError.ConnectionAborted, |
| 70 | 23 | | // Shutdown matches EPIPE and ConnectionReset matches ECONNRESET. Both are the result of the peer closing |
| 70 | 24 | | // non-gracefully the connection. EPIPE is returned if the socket is closed and the send buffer is empty |
| 70 | 25 | | // while ECONNRESET is returned if the send buffer is not empty. |
| 53 | 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, |
| 1 | 32 | | _ => IceRpcError.IceRpcError |
| 70 | 33 | | }; |
| | 34 | |
|
| 70 | 35 | | return new IceRpcException(errorCode, innerException); |
| 70 | 36 | | } |
| | 37 | | } |