| | | 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, 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 |
| | | 14 | | public sealed record Error |
| | | 15 | | { |
| | | 16 | | /// <summary>The "no error" value used by successful responses.</summary> |
| | 42 | 17 | | 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 | | ) |
| | 18 | 35 | | : 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 ] |
| | 608 | 46 | | public Error( |
| | 608 | 47 | | string code, |
| | 608 | 48 | | string? type, |
| | 608 | 49 | | string messageAr, |
| | 608 | 50 | | string message, |
| | 608 | 51 | | Severity? severity = null, |
| | 608 | 52 | | Exception? exception = null, |
| | 608 | 53 | | IReadOnlyDictionary<string, object>? additionalData = null |
| | 608 | 54 | | ) |
| | | 55 | | { |
| | 608 | 56 | | Code = code; |
| | 608 | 57 | | Type = type ?? "General"; |
| | 608 | 58 | | MessageAR = messageAr; |
| | 608 | 59 | | Message = message; |
| | 608 | 60 | | Severity = severity ?? Severity.Medium; |
| | 608 | 61 | | Exception = exception; |
| | 608 | 62 | | AdditionalData = additionalData ?? new Dictionary<string, object>(); |
| | 608 | 63 | | } |
| | | 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 ) |
| | 156 | 95 | | => 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 | | { |
| | 0 | 113 | | code = Code; |
| | 0 | 114 | | messageAR = MessageAR; |
| | 0 | 115 | | message = Message; |
| | 0 | 116 | | severity = Severity; |
| | 0 | 117 | | exception = Exception; |
| | 0 | 118 | | additionalData = AdditionalData; |
| | 0 | 119 | | } |
| | | 120 | | } |