コード例 #1
0
        /// <inheritdoc />
        public virtual async Task <IList <OperationContainer> > ProcessAsync(IList <OperationContainer> operations,
                                                                             CancellationToken cancellationToken)
        {
            if (operations == null)
            {
                throw new ArgumentNullException(nameof(operations));
            }

            _localIdValidator.Validate(operations);
            _localIdTracker.Reset();

            var results = new List <OperationContainer>();

            await using var transaction = await _operationsTransactionFactory.BeginTransactionAsync(cancellationToken);

            try
            {
                foreach (var operation in operations)
                {
                    operation.SetTransactionId(transaction.TransactionId);

                    await transaction.BeforeProcessOperationAsync(cancellationToken);

                    var result = await ProcessOperation(operation, cancellationToken);

                    results.Add(result);

                    await transaction.AfterProcessOperationAsync(cancellationToken);
                }

                await transaction.CommitAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JsonApiException exception)
            {
                foreach (var error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{results.Count}]" + error.Source.Pointer;
                }

                throw;
            }
            catch (Exception exception)
            {
                throw new JsonApiException(new Error(HttpStatusCode.InternalServerError)
                {
                    Title  = "An unhandled error occurred while processing an operation in this request.",
                    Detail = exception.Message,
                    Source =
                    {
                        Pointer = $"/atomic:operations[{results.Count}]"
                    }
                }, exception);
            }

            return(results);
        }
コード例 #2
0
        /// <inheritdoc />
        public virtual async Task <IList <OperationContainer> > ProcessAsync(IList <OperationContainer> operations, CancellationToken cancellationToken)
        {
            ArgumentGuard.NotNull(operations, nameof(operations));

            _localIdValidator.Validate(operations);
            _localIdTracker.Reset();

            var results = new List <OperationContainer>();

            await using IOperationsTransaction transaction = await _operationsTransactionFactory.BeginTransactionAsync(cancellationToken);

            try
            {
                foreach (OperationContainer operation in operations)
                {
                    operation.SetTransactionId(transaction.TransactionId);

                    await transaction.BeforeProcessOperationAsync(cancellationToken);

                    OperationContainer result = await ProcessOperationAsync(operation, cancellationToken);

                    results.Add(result);

                    await transaction.AfterProcessOperationAsync(cancellationToken);
                }

                await transaction.CommitAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JsonApiException exception)
            {
                foreach (Error error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{results.Count}]" + error.Source.Pointer;
                }

                throw;
            }
#pragma warning disable AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            catch (Exception exception)
#pragma warning restore AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            {
                throw new JsonApiException(new Error(HttpStatusCode.InternalServerError)
                {
                    Title  = "An unhandled error occurred while processing an operation in this request.",
                    Detail = exception.Message,
                    Source =
                    {
                        Pointer = $"/atomic:operations[{results.Count}]"
                    }
                }, exception);
            }

            return(results);
        }