~ruther/NosSmooth

ref: df3d5df306b523200b6a6489dd3c38e9b92318ca NosSmooth/Core/NosSmooth.Core/Extensions/ResultExtensions.cs -rw-r--r-- 3.1 KiB
df3d5df3 — František Boháček feat: update result logging mechanism 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  ResultExtensions.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  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;

namespace NosSmooth.Core.Extensions;

/// <summary>
/// Contains extension methods for <see cref="ResultExtensions"/>.
/// </summary>
public static class ResultExtensions
{
    /// <summary>
    /// Logs the given result if it isn't successful.
    /// </summary>
    /// <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 successful.</exception>
    public static void LogResultError(this ILogger logger, IResult result)
    {
        if (result.IsSuccess)
        {
            throw new InvalidOperationException("The result was successful, only unsuccessful results are supported.");
        }

        using StringWriter stringWriter = new StringWriter();
        using IndentedTextWriter logTextWriter = new IndentedTextWriter(stringWriter, " ");
        logTextWriter.WriteLine("Encountered an error");
        logTextWriter.Indent++;

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

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

    private static void LogAggreggateError(IndentedTextWriter logTextWriter, AggregateError error)
    {
        foreach (var result in error.Errors)
        {
            LogResultError(logTextWriter, 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.");
        }

        AppendErrorMessage(logTextWriter, result.Error);
        IResultError? lastError = result.Error;
        logTextWriter.Indent++;
        while ((result = result?.Inner) is not null && result.Error is not null)
        {
            // ReSharper disable once PossibleUnintendedReferenceComparison
            if (lastError == result.Error)
            {
                continue;
            }
            lastError = result.Error;

            // logTextWriter.Indent++;
            logTextWriter.Write("---> ");
            AppendErrorMessage(logTextWriter, result.Error);
        }
    }

    private static void AppendErrorMessage(IndentedTextWriter indentedTextWriter, IResultError error)
    {
        indentedTextWriter.WriteLine($"{error.GetType().FullName}: {error.Message}");
    }
}
Do not follow this link