| | | 1 | | #region Usings |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | #endregion |
| | | 5 | | |
| | | 6 | | namespace 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 |
| | | 15 | | public sealed record Error |
| | | 16 | | { |
| | | 17 | | /// <summary>The "no error" value used by successful responses.</summary> |
| | 3 | 18 | | 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 | | ) |
| | 18 | 36 | | : 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 ] |
| | 51 | 47 | | public Error( |
| | 51 | 48 | | string code, |
| | 51 | 49 | | string? type, |
| | 51 | 50 | | string messageAr, |
| | 51 | 51 | | string message, |
| | 51 | 52 | | Severity? severity = null, |
| | 51 | 53 | | Exception? exception = null, |
| | 51 | 54 | | IReadOnlyDictionary<string, object>? additionalData = null |
| | 51 | 55 | | ) |
| | | 56 | | { |
| | 51 | 57 | | Code = code; |
| | 51 | 58 | | Type = type ?? "General"; |
| | 51 | 59 | | MessageAR = messageAr; |
| | 51 | 60 | | Message = message; |
| | 51 | 61 | | Severity = severity ?? Severity.Medium; |
| | 51 | 62 | | Exception = exception; |
| | 51 | 63 | | AdditionalData = additionalData ?? new Dictionary<string, object>(); |
| | 51 | 64 | | } |
| | | 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 ) |
| | 9 | 116 | | => 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 | | { |
| | 0 | 134 | | code = Code; |
| | 0 | 135 | | messageAR = MessageAR; |
| | 0 | 136 | | message = Message; |
| | 0 | 137 | | severity = Severity; |
| | 0 | 138 | | exception = Exception; |
| | 0 | 139 | | additionalData = AdditionalData; |
| | 0 | 140 | | } |
| | | 141 | | } |