System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException C# (CSharp) Method

ThrowIfConvertibleException() private method

private ThrowIfConvertibleException ( string methodName, Exception exception, CancellationToken cancellationToken, bool aborted ) : void
methodName string
exception Exception
cancellationToken System.Threading.CancellationToken
aborted bool
return void
        private void ThrowIfConvertibleException(string methodName,
            Exception exception,
            CancellationToken cancellationToken,
            bool aborted)
        {
            Debug.Assert(exception != null, "'exception' MUST NOT be NULL.");

            if (NetEventSource.IsEnabled && !string.IsNullOrEmpty(methodName))
            {
                NetEventSource.Error(this, $"methodName: {methodName}, exception: {exception}");
            }

            OperationCanceledException operationCanceledException = exception as OperationCanceledException;
            if (operationCanceledException != null)
            {
                if (cancellationToken.IsCancellationRequested ||
                    !aborted)
                {
                    return;
                }
                ThrowIfAborted(aborted, exception);
            }

            WebSocketException convertedException = exception as WebSocketException;
            if (convertedException != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ThrowIfAborted(aborted, convertedException);
                return;
            }

            SocketException socketException = exception as SocketException;
            if (socketException != null)
            {
                convertedException = new WebSocketException(socketException.NativeErrorCode, socketException);
            }

            HttpListenerException httpListenerException = exception as HttpListenerException;
            if (httpListenerException != null)
            {
                convertedException = new WebSocketException(httpListenerException.ErrorCode, httpListenerException);
            }

            IOException ioException = exception as IOException;
            if (ioException != null)
            {
                socketException = exception.InnerException as SocketException;
                if (socketException != null)
                {
                    convertedException = new WebSocketException(socketException.NativeErrorCode, ioException);
                }
            }

            if (convertedException != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ThrowIfAborted(aborted, convertedException);
                throw convertedException;
            }

            AggregateException aggregateException = exception as AggregateException;
            if (aggregateException != null)
            {
                // Collapse possibly nested graph into a flat list.
                // Empty inner exception list is unlikely but possible via public api.
                ReadOnlyCollection<Exception> unwrappedExceptions = aggregateException.Flatten().InnerExceptions;
                if (unwrappedExceptions.Count == 0)
                {
                    return;
                }

                foreach (Exception unwrappedException in unwrappedExceptions)
                {
                    ThrowIfConvertibleException(null, unwrappedException, cancellationToken, aborted);
                }
            }
        }