Ejemplo n.º 1
0
        public void NullParameterException1()
        {
            var exception = new NullParameterException("The parameter name");

            Assert.AreEqual("The parameter name", exception.ParameterName);
            Assert.AreEqual("The parameter 'The parameter name' cannot be null.", exception.Message);
        }
Ejemplo n.º 2
0
        public async Task <OperationResult <TEntity> > SaveAsync(string identifier, string partitionKey, TEntity document, string eTag)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                NullParameterException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    nameof(identifier)
                    );
            }

            if (document == null)
            {
                NullParameterException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    nameof(document)
                    );
            }

            ItemResponse <TEntity> response = null;

            PartitionKey?pkey = null;

            if (!string.IsNullOrEmpty(partitionKey))
            {
                pkey = new PartitionKey(partitionKey);
            }

            try
            {
                logger.SaveRequested(containerName, partitionKey, identifier.ToString());

                response = await(await container).UpsertItemAsync <TEntity>(document, pkey, GetRequestOptions(eTag));
            }
            catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.PreconditionFailed)
            {
                logger.SaveFailed(containerName, partitionKey, identifier.ToString(), ex.Message, ex);

                DocumentHasChangedException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    eTag
                    );
            }
            catch (CosmosException ex) when(((int)ex.StatusCode) == 429)
            {
                logger.SaveFailed(containerName, partitionKey, identifier.ToString(), ex.Message, ex);

                CosmosDBRequestCapacityExceededException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    ex
                    );
            }
            catch (CosmosException ex)
            {
                logger.SaveFailed(containerName, partitionKey, identifier.ToString(), ex.Message, ex);

                DocumentUpsertException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    eTag,
                    ex
                    );
            }
            catch (Exception ex)
            {
                logger.SaveFailed(containerName, partitionKey, identifier.ToString(), ex.Message, ex);

                DocumentUpsertException.Raise(
                    configuration.Value.DatabaseAccountUri,
                    configuration.Value.DatabaseName,
                    containerName,
                    partitionKey,
                    identifier.ToString(),
                    eTag,
                    ex
                    );
            }

            var isSuccessfull =
                response?.StatusCode == System.Net.HttpStatusCode.OK ||
                response?.StatusCode == System.Net.HttpStatusCode.Created;

            if (isSuccessfull)
            {
                logger.SaveCompleted(containerName, partitionKey, identifier.ToString(), response.RequestCharge);
            }
            else
            {
                logger.SaveFailed(containerName, partitionKey, identifier.ToString(), $"Response code is {response?.StatusCode}", null);
            }


            return(new OperationResult <TEntity>(
                       isSuccessfull,
                       isSuccessfull ? response.Resource : default,