Exemple #1
0
    private async Task SendAsync(List <KuduOperation> queue)
    {
        try
        {
            await _client.WriteAsync(queue, _options.ExternalConsistencyMode, _txnId)
            .ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            _logger.ExceptionFlushingSessionData(ex, queue.Count, _txnId);

            var exceptionHandler = _options.ExceptionHandler;
            if (exceptionHandler is not null)
            {
                var queueCopy        = new List <KuduOperation>(queue);
                var exceptionContext = new SessionExceptionContext(ex, queueCopy);

                try
                {
                    await exceptionHandler(exceptionContext).ConfigureAwait(false);
                }
                catch { }
            }
        }
    }
    public async Task TestExceptionCallback()
    {
        int numCallbacks = 0;
        SessionExceptionContext sessionContext = null;

        var builder = ClientTestUtil.GetBasicSchema()
                      .SetTableName(nameof(TestExceptionCallback));

        var table = await _client.CreateTableAsync(builder);

        var row1 = ClientTestUtil.CreateBasicSchemaInsert(table, 1);
        var row2 = ClientTestUtil.CreateBasicSchemaInsert(table, 1);

        var sessionOptions = new KuduSessionOptions
        {
            ExceptionHandler = HandleSessionExceptionAsync
        };

        await using var session = _client.NewSession(sessionOptions);

        await session.EnqueueAsync(row1);

        await session.FlushAsync();

        await session.EnqueueAsync(row2);

        await session.FlushAsync();

        ValueTask HandleSessionExceptionAsync(SessionExceptionContext context)
        {
            numCallbacks++;
            sessionContext = context;
            return(new ValueTask());
        }

        Assert.Equal(1, numCallbacks);

        var errorRow = Assert.Single(sessionContext.Rows);

        Assert.Same(row2, errorRow);

        var exception    = Assert.IsType <KuduWriteException>(sessionContext.Exception);
        var exceptionRow = Assert.Single(exception.PerRowErrors);

        Assert.True(exceptionRow.IsAlreadyPresent);
    }