|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | + |
| 4 | +namespace Foundatio.CommandQuery.Results; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Represents an error with a message and associated metadata. |
| 8 | +/// </summary> |
| 9 | +[ImmutableObject(true)] |
| 10 | +public class Error : IError |
| 11 | +{ |
| 12 | + private static readonly int DefaultStatus = 500; |
| 13 | + private const string DefaultError = "An unknown error occurred"; |
| 14 | + |
| 15 | + public static Error Empty { get; } = new(); |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="Error" /> class. |
| 19 | + /// </summary> |
| 20 | + public Error() : this(DefaultStatus, DefaultError) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message.</summary> |
| 25 | + /// <param name="status">The status code generated by the origin server</param> |
| 26 | + /// <param name="message">The error message.</param> |
| 27 | + public Error(int status, string message) |
| 28 | + { |
| 29 | + Status = status; |
| 30 | + Message = message; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified metadata.</summary> |
| 34 | + /// <param name="extensions">The metadata associated with the error.</param> |
| 35 | + public Error((string Key, object? Value) extensions) |
| 36 | + : this(DefaultStatus, DefaultError, extensions) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message and metadata.</summary> |
| 41 | + /// <param name="status">The status code generated by the origin server</param> |
| 42 | + /// <param name="message">The error message.</param> |
| 43 | + /// <param name="extensions">The metadata associated with the error.</param> |
| 44 | + public Error(int status, string message, (string Key, object? Value) extensions) |
| 45 | + { |
| 46 | + Status = status; |
| 47 | + Message = message; |
| 48 | + |
| 49 | + Extensions = new Dictionary<string, object?> |
| 50 | + { |
| 51 | + [extensions.Key] = extensions.Value |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified metadata.</summary> |
| 56 | + /// <param name="extensions">The metadata associated with the error.</param> |
| 57 | + public Error(IReadOnlyDictionary<string, object?> extensions) |
| 58 | + : this(DefaultStatus, DefaultError, extensions) |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary>Initializes a new instance of the <see cref="Error" /> class with the specified error message and metadata.</summary> |
| 63 | + /// <param name="status">The status code generated by the origin server</param> |
| 64 | + /// <param name="message">The error message.</param> |
| 65 | + /// <param name="extensions">The metadata associated with the error.</param> |
| 66 | + public Error(int status, string message, IReadOnlyDictionary<string, object?> extensions) |
| 67 | + { |
| 68 | + Status = status; |
| 69 | + Message = message; |
| 70 | + Extensions = extensions; |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc /> |
| 74 | + public int Status { get; } |
| 75 | + |
| 76 | + /// <inheritdoc /> |
| 77 | + public string Message { get; } |
| 78 | + |
| 79 | + /// <inheritdoc /> |
| 80 | + public IReadOnlyDictionary<string, object?>? Extensions { get; } |
| 81 | +} |
0 commit comments