~ruther/NosSmooth

4aad2ad36b1f488f98e969bcc17ae2ca0f077d92 — František Boháček 3 years ago e5c6fc7
feat: add support for logging nested results
1 files changed, 33 insertions(+), 14 deletions(-)

M Core/NosSmooth.Core/Extensions/ResultExtensions.cs
M Core/NosSmooth.Core/Extensions/ResultExtensions.cs => Core/NosSmooth.Core/Extensions/ResultExtensions.cs +33 -14
@@ 5,6 5,8 @@
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
using Remora.Results;


@@ 17,51 19,68 @@ namespace NosSmooth.Core.Extensions;
public static class ResultExtensions
{
    /// <summary>
    /// Logs the given result if it is errorful.
    /// Logs the given result if it isn't successful.
    /// </summary>
    /// <param name="logger">The logger.</param>
    /// <param name="logger">The logger to log with.</param>
    /// <param name="result">The result to log.</param>
    /// <exception cref="InvalidOperationException">Thrown if the result was unsuccessful.</exception>
    /// <exception cref="InvalidOperationException">Thrown if the result was successful.</exception>
    public static void LogResultError(this ILogger logger, IResult result)
    {
        if (result.IsSuccess)
        {
            throw new InvalidOperationException("The result was successful, it has to be unsuccessful to log it.");
            throw new InvalidOperationException("The result was successful, only unsuccessful results are supported.");
        }

        using StringWriter stringWriter = new StringWriter();
        using IndentedTextWriter logTextWriter = new IndentedTextWriter(stringWriter, " ");
        logTextWriter.Write("Encountered an error: ");

        LogResultError(logTextWriter, result);
        logger.LogError(stringWriter.ToString());
    }

    private static void LogResultError(IndentedTextWriter logTextWriter, IResult result)
    {
        switch (result.Error)
        {
            case AggregateError aggregateError:
                logger.LogAggreggateError(aggregateError);
                LogAggreggateError(logTextWriter, aggregateError);
                break;
            default:
                logger.LogNestedError(result);
                LogNestedError(logTextWriter, result);
                break;
        }
    }

    private static void LogAggreggateError(this ILogger logger, AggregateError error)
    private static void LogAggreggateError(IndentedTextWriter logTextWriter, AggregateError error)
    {
        foreach (var result in error.Errors)
        {
            logger.LogResultError(result);
            LogResultError(logTextWriter, result);
        }
    }

    private static void LogNestedError(this ILogger logger, IResult? result)
    private static void LogNestedError(IndentedTextWriter logTextWriter, IResult? result)
    {
        if ((result?.IsSuccess ?? true) || result.Error is null)
        {
            throw new InvalidOperationException("The result was successful, it has to be unsuccessful to log it.");
        }

        var stringBuilder = new StringBuilder(result.Error.Message);
        int index = 0;
        logTextWriter.WriteLine(result.Error.Message);
        IResultError? lastError = result.Error;
        while ((result = result?.Inner) is not null && result.Error is not null)
        {
            stringBuilder.Append($"\n  {index++}: {result.Error.Message}");
        }
            // ReSharper disable once PossibleUnintendedReferenceComparison
            if (lastError == result.Error)
            {
                continue;
            }
            lastError = result.Error;

        logger.LogError(stringBuilder.ToString());
            logTextWriter.WriteLine("--- See the inner error ---");
            logTextWriter.Indent++;
            logTextWriter.WriteLine(result.Error.Message);
        }
    }
}
\ No newline at end of file

Do not follow this link