private static bool TryCreateFromExceptionWithStackTrace(
            ExceptionWithStackTraceException exceptionWithStackTrace,
            ITrace trace,
            out CosmosException cosmosException)
        {
            // Use the original stack trace from the inner exception.
            if (exceptionWithStackTrace.InnerException is Microsoft.Azure.Documents.DocumentClientException ||
                exceptionWithStackTrace.InnerException is CosmosException)
            {
                return(ExceptionToCosmosException.TryCreateFromException(
                           exceptionWithStackTrace.InnerException,
                           trace,
                           out cosmosException));
            }

            if (!ExceptionToCosmosException.TryCreateFromException(
                    exceptionWithStackTrace.InnerException,
                    trace,
                    out cosmosException))
            {
                return(false);
            }

            cosmosException = CosmosExceptionFactory.Create(
                cosmosException.StatusCode,
                cosmosException.Message,
                exceptionWithStackTrace.StackTrace,
                headers: cosmosException.Headers,
                cosmosException.Trace,
                cosmosException.Error,
                cosmosException.InnerException);
            return(true);
        }
        public static bool TryCreateFromException(
            Exception exception,
            ITrace trace,
            out CosmosException cosmosException)
        {
            if (exception is CosmosException ce)
            {
                cosmosException = ce;
                return(true);
            }

            if (exception is Microsoft.Azure.Documents.DocumentClientException documentClientException)
            {
                cosmosException = CreateFromDocumentClientException(documentClientException, trace);
                return(true);
            }

            if (exception is QueryException queryException)
            {
                cosmosException = queryException.Accept(QueryExceptionConverter.Singleton, trace);
                return(true);
            }

            if (exception is ChangeFeedException changeFeedException)
            {
                cosmosException = changeFeedException.Accept(ChangeFeedExceptionConverter.Singleton, trace);
                return(true);
            }

            if (exception is ExceptionWithStackTraceException exceptionWithStackTrace)
            {
                return(TryCreateFromExceptionWithStackTrace(exceptionWithStackTrace, trace, out cosmosException));
            }

            if (exception.InnerException != null)
            {
                // retry with the inner exception
                return(ExceptionToCosmosException.TryCreateFromException(
                           exception.InnerException,
                           trace,
                           out cosmosException));
            }

            cosmosException = default;
            return(false);
        }