|
| 1 | +// Copyright © 2021-Present Neuroglia SRL. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"), |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +using System.Runtime.Serialization; |
| 15 | +using System.Text.Json.Serialization; |
| 16 | + |
| 17 | +namespace Neuroglia; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Represents an object used to describe a problem, as defined by <see href="https://www.rfc-editor.org/rfc/rfc7807">RFC 7807</see> |
| 21 | +/// </summary> |
| 22 | +[DataContract] |
| 23 | +public record ProblemDetails |
| 24 | + : IExtensible |
| 25 | +{ |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initialize a new <see cref="ProblemDetails"/> |
| 29 | + /// </summary> |
| 30 | + public ProblemDetails() { } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initialize a new <see cref="ProblemDetails"/> |
| 34 | + /// </summary> |
| 35 | + /// <param name="type">An uri that reference the type of the described problem</param> |
| 36 | + /// <param name="title">A short, human-readable summary of the problem type.It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization</param> |
| 37 | + /// <param name="status">The status code produced by the described problem</param> |
| 38 | + /// <param name="detail">A human-readable explanation specific to this occurrence of the problem</param> |
| 39 | + /// <param name="instance">A <see cref="Uri"/> reference that identifies the specific occurrence of the problem.It may or may not yield further information if dereferenced</param> |
| 40 | + /// <param name="errors">An optional collection containing error messages mapped per error code</param> |
| 41 | + /// <param name="extensionData">A mapping containing problem details extension data, if any</param> |
| 42 | + public ProblemDetails(Uri type, string title, int status, string? detail = null, Uri? instance = null, IEnumerable<KeyValuePair<string, string[]>>? errors = null, IDictionary<string, object>? extensionData = null) |
| 43 | + { |
| 44 | + this.Type = type ?? throw new ArgumentNullException(nameof(type)); |
| 45 | + this.Title = title ?? throw new ArgumentNullException(nameof(title)); |
| 46 | + this.Status = status; |
| 47 | + this.Detail = detail; |
| 48 | + this.Instance = instance; |
| 49 | + this.Errors = errors?.WithValueSemantics(); |
| 50 | + this.ExtensionData = extensionData; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets/sets an uri that reference the type of the described problem. |
| 55 | + /// </summary> |
| 56 | + [DataMember(Order = 1, Name = "type"), JsonPropertyName("type")] |
| 57 | + public virtual Uri? Type { get; set; } = null!; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets/sets a short, human-readable summary of the problem type.It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization. |
| 61 | + /// </summary> |
| 62 | + [DataMember(Order = 2, Name = "title"), JsonPropertyName("title")] |
| 63 | + public virtual string? Title { get; set; } = null!; |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets/sets the status code produced by the described problem |
| 67 | + /// </summary> |
| 68 | + [DataMember(Order = 3, Name = "status"), JsonPropertyName("status"),] |
| 69 | + public virtual int Status { get; set; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets/sets a human-readable explanation specific to this occurrence of the problem. |
| 73 | + /// </summary> |
| 74 | + [DataMember(Order = 4, Name = "detail"), JsonPropertyName("detail")] |
| 75 | + public virtual string? Detail { get; set; } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets/sets a <see cref="Uri"/> reference that identifies the specific occurrence of the problem.It may or may not yield further information if dereferenced. |
| 79 | + /// </summary> |
| 80 | + [DataMember(Order = 5, Name = "instance"), JsonPropertyName("instance")] |
| 81 | + public virtual Uri? Instance { get; set; } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets/sets an optional collection containing error messages mapped per error code |
| 85 | + /// </summary> |
| 86 | + [DataMember(Order = 6, Name = "errors"), JsonPropertyName("errors")] |
| 87 | + public virtual EquatableList<KeyValuePair<string, string[]>>? Errors { get; set; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets/sets a mapping containing problem details extension data, if any |
| 91 | + /// </summary> |
| 92 | + [DataMember(Order = 7, Name = "extensionData"), JsonExtensionData] |
| 93 | + public virtual IDictionary<string, object>? ExtensionData { get; set; } |
| 94 | + |
| 95 | + /// <inheritdoc/> |
| 96 | + public override string ToString() => this.Detail ?? Environment.NewLine + string.Join(Environment.NewLine, this.Errors?.Select(e => string.Join(Environment.NewLine, e.Value.Select(v => $"{e.Key}: {v}")))!); |
| 97 | + |
| 98 | +} |
0 commit comments