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 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));
        }