< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Line coverage
74%
Covered lines: 20
Uncovered lines: 7
Coverable lines: 27
Total lines: 120
Line coverage: 74%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%66100%
Unknown(...)100%11100%
Deconstruct(...)100%210%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.LayerResponse/Errors/Error.cs

#LineLine coverage
 1#region Usings
 2using System.Diagnostics.CodeAnalysis;
 3using System.Text.Json.Serialization;
 4#endregion
 5
 6namespace ZhEaIsNsAaBn.Utilities;
 7
 8/// <summary>
 9/// A coded, bilingual error carried by <see cref="LayerResponse"/>. Clients see <see cref="Code"/>, <see cref="Message"
 10/// and <see cref="MessageAR"/>; <see cref="Exception"/> and <see cref="AdditionalData"/> are kept for logs only.
 11/// </summary>
 12[ SuppressMessage( "Naming", "CA1716:Identifiers should not match keywords",
 13                   Justification = "Error is the house name for the result error type (STYLE_GUIDE §6); C# consumers onl
 14public sealed record Error
 15{
 16    /// <summary>The "no error" value used by successful responses.</summary>
 4217    public static readonly Error None = new( string.Empty, string.Empty, string.Empty, string.Empty, Severity.Unknown );
 18
 19
 20    /// <summary>Creates an error with the default <see cref="Type"/> (<c>General</c>).</summary>
 21    /// <param name="code">Stable error code, usually from <c>nameof(...)</c>.</param>
 22    /// <param name="messageAr">Arabic message for users.</param>
 23    /// <param name="message">English message for users.</param>
 24    /// <param name="severity">Severity. Default: <see cref="Severity.Medium"/>.</param>
 25    /// <param name="exception">The exception behind the error, if any. Never serialized.</param>
 26    /// <param name="additionalData">Extra values for logs. Never serialized. Default: empty.</param>
 27    public Error(
 28            string code,
 29            string messageAr,
 30            string message,
 31            Severity? severity = null,
 32            Exception? exception = null,
 33            IReadOnlyDictionary<string, object>? additionalData = null
 34        )
 1835        : this( code, null, messageAr, message, severity, exception, additionalData ) { }
 36
 37    /// <summary>Creates an error with an explicit <see cref="Type"/>.</summary>
 38    /// <param name="code">Stable error code, usually from <c>nameof(...)</c>.</param>
 39    /// <param name="type">Error category. Default: <c>General</c>.</param>
 40    /// <param name="messageAr">Arabic message for users.</param>
 41    /// <param name="message">English message for users.</param>
 42    /// <param name="severity">Severity. Default: <see cref="Severity.Medium"/>.</param>
 43    /// <param name="exception">The exception behind the error, if any. Never serialized.</param>
 44    /// <param name="additionalData">Extra values for logs. Never serialized. Default: empty.</param>
 45    [ JsonConstructor ]
 60846    public Error(
 60847            string code,
 60848            string? type,
 60849            string messageAr,
 60850            string message,
 60851            Severity? severity = null,
 60852            Exception? exception = null,
 60853            IReadOnlyDictionary<string, object>? additionalData = null
 60854        )
 55    {
 60856        Code           = code;
 60857        Type           = type ?? "General";
 60858        MessageAR      = messageAr;
 60859        Message        = message;
 60860        Severity       = severity ?? Severity.Medium;
 60861        Exception      = exception;
 60862        AdditionalData = additionalData ?? new Dictionary<string, object>();
 60863    }
 64
 65
 66    /// <summary>Stable error code, e.g. <c>NotFound</c>.</summary>
 67    public string Code { get; init; }
 68
 69    /// <summary>Error category. Default: <c>General</c>.</summary>
 70    public string Type { get; init; }
 71
 72    /// <summary>Arabic message for users.</summary>
 73    public string MessageAR { get; init; }
 74
 75    /// <summary>English message for users.</summary>
 76    public string Message { get; init; }
 77
 78    /// <summary>How serious the error is.</summary>
 79    public Severity Severity { get; init; }
 80
 81    /// <summary>The exception behind the error, for logs. Never serialized (STYLE_GUIDE §6).</summary>
 82    [ JsonIgnore ]
 83    public Exception? Exception { get; init; }
 84
 85    /// <summary>Extra values for logs. Never serialized. Empty when not set.</summary>
 86    [ JsonIgnore ]
 87    public IReadOnlyDictionary<string, object> AdditionalData { get; init; }
 88
 89
 90    /// <summary>Creates the generic "unknown error" with fixed bilingual text, keeping the exception for logs.</summary
 91    /// <param name="exception">The exception that was caught.</param>
 92    /// <param name="additionalData">Extra values for logs, if any.</param>
 93    /// <returns>An error with code <c>Unknown</c> and <see cref="Severity.Unknown"/>.</returns>
 94    public static Error Unknown( Exception exception, IReadOnlyDictionary<string, object>? additionalData = null )
 15695        => new( nameof( Unknown ), string.Empty, "حدث خطأ غير معروف", "An unknown error occurred", Severity.Unknown, exc
 96
 97    /// <summary>Splits the error into its parts.</summary>
 98    /// <param name="code">Receives <see cref="Code"/>.</param>
 99    /// <param name="messageAR">Receives <see cref="MessageAR"/>.</param>
 100    /// <param name="message">Receives <see cref="Message"/>.</param>
 101    /// <param name="severity">Receives <see cref="Severity"/>.</param>
 102    /// <param name="exception">Receives <see cref="Exception"/>.</param>
 103    /// <param name="additionalData">Receives <see cref="AdditionalData"/>.</param>
 104    public void Deconstruct(
 105            out string code,
 106            out string messageAR,
 107            out string message,
 108            out Severity severity,
 109            out Exception? exception,
 110            out IReadOnlyDictionary<string, object> additionalData
 111        )
 112    {
 0113        code           = Code;
 0114        messageAR      = MessageAR;
 0115        message        = Message;
 0116        severity       = Severity;
 0117        exception      = Exception;
 0118        additionalData = AdditionalData;
 0119    }
 120}