< Summary - ZhEaIsNsAaBn.Essentials coverage

Information
Class: ZhEaIsNsAaBn.Utilities.Error
Assembly: ZhEaIsNsAaBn.LayerResponse
File(s): /_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/Errors/Error.cs
Line coverage
74%
Covered lines: 20
Uncovered lines: 7
Coverable lines: 27
Total lines: 141
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)

/_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/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 error carried by <see cref="LayerResponse"/>. <see cref="Code"/> is the stable key clients read; <see cref="
 10/// English for developers and logs. Text for users comes from <see cref="LocalizedMessage"/>, which the API edge fills 
 11/// language (<see cref="IErrorLocalizer"/>). <see cref="Exception"/> and <see cref="AdditionalData"/> are kept for logs
 12/// </summary>
 13[ SuppressMessage( "Naming", "CA1716:Identifiers should not match keywords",
 14                   Justification = "Error is the house name for the result error type (STYLE_GUIDE §6); C# consumers onl
 15public sealed record Error
 16{
 17    /// <summary>The "no error" value used by successful responses.</summary>
 318    public static readonly Error None = new( string.Empty, string.Empty, string.Empty, string.Empty, Severity.Unknown );
 19
 20
 21    /// <summary>Creates an error with the default <see cref="Type"/> (<c>General</c>).</summary>
 22    /// <param name="code">Stable error code, usually from <c>nameof(...)</c>.</param>
 23    /// <param name="messageAr">Arabic message for users.</param>
 24    /// <param name="message">English message for users.</param>
 25    /// <param name="severity">Severity. Default: <see cref="Severity.Medium"/>.</param>
 26    /// <param name="exception">The exception behind the error, if any. Never serialized.</param>
 27    /// <param name="additionalData">Extra values for logs. Never serialized. Default: empty.</param>
 28    public Error(
 29            string code,
 30            string messageAr,
 31            string message,
 32            Severity? severity = null,
 33            Exception? exception = null,
 34            IReadOnlyDictionary<string, object>? additionalData = null
 35        )
 1836        : this( code, null, messageAr, message, severity, exception, additionalData ) { }
 37
 38    /// <summary>Creates an error with an explicit <see cref="Type"/>.</summary>
 39    /// <param name="code">Stable error code, usually from <c>nameof(...)</c>.</param>
 40    /// <param name="type">Error category. Default: <c>General</c>.</param>
 41    /// <param name="messageAr">Arabic message for users.</param>
 42    /// <param name="message">English message for users.</param>
 43    /// <param name="severity">Severity. Default: <see cref="Severity.Medium"/>.</param>
 44    /// <param name="exception">The exception behind the error, if any. Never serialized.</param>
 45    /// <param name="additionalData">Extra values for logs. Never serialized. Default: empty.</param>
 46    [ JsonConstructor ]
 5147    public Error(
 5148            string code,
 5149            string? type,
 5150            string messageAr,
 5151            string message,
 5152            Severity? severity = null,
 5153            Exception? exception = null,
 5154            IReadOnlyDictionary<string, object>? additionalData = null
 5155        )
 56    {
 5157        Code           = code;
 5158        Type           = type ?? "General";
 5159        MessageAR      = messageAr;
 5160        Message        = message;
 5161        Severity       = severity ?? Severity.Medium;
 5162        Exception      = exception;
 5163        AdditionalData = additionalData ?? new Dictionary<string, object>();
 5164    }
 65
 66
 67    /// <summary>Stable error code, e.g. <c>NotFound</c>.</summary>
 68    public string Code { get; init; }
 69
 70    /// <summary>Error category. Default: <c>General</c>.</summary>
 71    public string Type { get; init; }
 72
 73    /// <summary>
 74    /// Arabic message. <b>Transitional:</b> <see cref="ErrorLocalizer"/> uses it as the <c>ar</c> text when no resource
 75    /// removed once every error catalog ships its own <c>.resx</c> files (before the first release).
 76    /// </summary>
 77    public string MessageAR { get; init; }
 78
 79    /// <summary>
 80    /// English message for developers and logs; also the last fallback for users. May hold named placeholders such as
 81    /// <c>{Field}</c>, filled from <see cref="Arguments"/>.
 82    /// </summary>
 83    public string Message { get; init; }
 84
 85    /// <summary>
 86    /// Values for the named placeholders in the message, e.g. <c>Field</c> → <c>"Email"</c>. Serialized, so a client th
 87    /// codes itself can fill them too — put only values that are safe to show the caller. <see langword="null"/> when n
 88    /// </summary>
 89    [ JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull ) ]
 90    public IReadOnlyDictionary<string, object?>? Arguments { get; init; }
 91
 92    /// <summary>
 93    /// The message for users in the request's language. Set by the API edge (<see cref="IErrorLocalizer"/>); <see langw
 94    /// inside the application.
 95    /// </summary>
 96    [ JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull ) ]
 97    public LocalizedMessage? LocalizedMessage { get; init; }
 98
 99    /// <summary>How serious the error is.</summary>
 100    public Severity Severity { get; init; }
 101
 102    /// <summary>The exception behind the error, for logs. Never serialized (STYLE_GUIDE §6).</summary>
 103    [ JsonIgnore ]
 104    public Exception? Exception { get; init; }
 105
 106    /// <summary>Extra values for logs. Never serialized. Empty when not set.</summary>
 107    [ JsonIgnore ]
 108    public IReadOnlyDictionary<string, object> AdditionalData { get; init; }
 109
 110
 111    /// <summary>Creates the generic "unknown error" with fixed bilingual text, keeping the exception for logs.</summary
 112    /// <param name="exception">The exception that was caught.</param>
 113    /// <param name="additionalData">Extra values for logs, if any.</param>
 114    /// <returns>An error with code <c>Unknown</c> and <see cref="Severity.Unknown"/>.</returns>
 115    public static Error Unknown( Exception exception, IReadOnlyDictionary<string, object>? additionalData = null )
 9116        => new( nameof( Unknown ), string.Empty, "حدث خطأ غير معروف", "An unknown error occurred", Severity.Unknown, exc
 117
 118    /// <summary>Splits the error into its parts.</summary>
 119    /// <param name="code">Receives <see cref="Code"/>.</param>
 120    /// <param name="messageAR">Receives <see cref="MessageAR"/>.</param>
 121    /// <param name="message">Receives <see cref="Message"/>.</param>
 122    /// <param name="severity">Receives <see cref="Severity"/>.</param>
 123    /// <param name="exception">Receives <see cref="Exception"/>.</param>
 124    /// <param name="additionalData">Receives <see cref="AdditionalData"/>.</param>
 125    public void Deconstruct(
 126            out string code,
 127            out string messageAR,
 128            out string message,
 129            out Severity severity,
 130            out Exception? exception,
 131            out IReadOnlyDictionary<string, object> additionalData
 132        )
 133    {
 0134        code           = Code;
 0135        messageAR      = MessageAR;
 0136        message        = Message;
 0137        severity       = Severity;
 0138        exception      = Exception;
 0139        additionalData = AdditionalData;
 0140    }
 141}