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 CosmosException CreateFromException(Exception exception)
        {
            if (exception is CosmosException cosmosException)
            {
                return(cosmosException);
            }

            if (exception is Microsoft.Azure.Documents.DocumentClientException documentClientException)
            {
                return(CreateFromDocumentClientException(documentClientException));
            }

            if (exception is QueryException queryException)
            {
                return(queryException.Accept(QueryExceptionConverter.Singleton));
            }

            if (exception is ExceptionWithStackTraceException exceptionWithStackTrace)
            {
                return(CreateFromExceptionWithStackTrace(exceptionWithStackTrace));
            }

            if (exception.InnerException != null)
            {
                // retry with the inner exception
                return(ExceptionToCosmosException.CreateFromException(exception.InnerException));
            }

            return(CosmosExceptionFactory.CreateInternalServerErrorException(
                       subStatusCode: default,
        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);
        }
        public static CosmosException CreateFromException(Exception exception)
        {
            if (exception is CosmosException cosmosException)
            {
                return(cosmosException);
            }

            if (exception is Microsoft.Azure.Documents.DocumentClientException documentClientException)
            {
                return(CreateFromDocumentClientException(documentClientException));
            }

            if (exception is QueryException queryException)
            {
                return(queryException.Accept(QueryExceptionConverter.Singleton));
            }

            if (exception is ChangeFeedException changeFeedException)
            {
                return(changeFeedException.Accept(ChangeFeedExceptionConverter.Singleton));
            }

            if (exception is ExceptionWithStackTraceException exceptionWithStackTrace)
            {
                return(CreateFromExceptionWithStackTrace(exceptionWithStackTrace));
            }

            if (exception.InnerException != null)
            {
                // retry with the inner exception
                return(ExceptionToCosmosException.CreateFromException(exception.InnerException));
            }

            return(CosmosExceptionFactory.CreateInternalServerErrorException(
                       message: exception.Message,
                       stackTrace: exception.StackTrace,
                       headers: new Headers()
            {
                ActivityId = EmptyGuidString,
            },
                       trace: NoOpTrace.Singleton,
                       innerException: exception));
        }
        private static CosmosException CreateFromExceptionWithStackTrace(ExceptionWithStackTraceException exceptionWithStackTrace)
        {
            // Use the original stack trace from the inner exception.
            if (exceptionWithStackTrace.InnerException is Microsoft.Azure.Documents.DocumentClientException ||
                exceptionWithStackTrace.InnerException is CosmosException)
            {
                return(ExceptionToCosmosException.CreateFromException(exceptionWithStackTrace.InnerException));
            }

            CosmosException cosmosException = ExceptionToCosmosException.CreateFromException(exceptionWithStackTrace.InnerException);

            return(CosmosExceptionFactory.Create(
                       cosmosException.StatusCode,
                       cosmosException.Message,
                       exceptionWithStackTrace.StackTrace,
                       headers: cosmosException.Headers,
                       cosmosException.Trace,
                       cosmosException.Error,
                       cosmosException.InnerException));
        }