| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MultipleHttpException.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Text; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Http.Exceptions; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents an exception that encapsulates multiple HTTP errors, allowing for comprehensive error reporting in scenar |
| | | 17 | | /// </summary> |
| | | 18 | | public class MultipleHttpException : Exception |
| | | 19 | | { |
| | 12 | 20 | | private readonly List<HttpError> _errors = []; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets a read-only collection of <see cref="HttpError"/> instances that represent the individual errors associated |
| | | 24 | | /// </summary> |
| | 18 | 25 | | public IReadOnlyCollection<HttpError> Errors => _errors.AsReadOnly(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with no parameters. This constructor |
| | | 29 | | /// </summary> |
| | 0 | 30 | | public MultipleHttpException() { } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a specified error message. This |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | | 36 | | public MultipleHttpException(string message) |
| | 0 | 37 | | : base(message) |
| | | 38 | | { |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a collection of error messages. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="errors">A collection of error messages that describe the individual errors.</param> |
| | | 45 | | public MultipleHttpException(IEnumerable<string> errors) |
| | 0 | 46 | | : this(string.Empty, errors) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a collection of <see cref="Http |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="errors">A collection of <see cref="HttpError"/> instances that describe the individual errors.</par |
| | | 54 | | public MultipleHttpException(IEnumerable<HttpError> errors) |
| | 3 | 55 | | : this(string.Empty, errors) |
| | | 56 | | { |
| | 3 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a specified error message and a |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | | 63 | | /// <param name="errors">A collection of <see cref="HttpError"/> instances that describe the individual errors.</par |
| | | 64 | | public MultipleHttpException(string message, IEnumerable<HttpError> errors) |
| | 9 | 65 | | : base(message) |
| | 9 | 66 | | => _errors.AddRange(errors); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a specified error message and a |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | | 72 | | /// <param name="errors">A collection of error messages that describe the individual errors.</param> |
| | | 73 | | public MultipleHttpException(string message, IEnumerable<string> errors) |
| | 3 | 74 | | : base(message) |
| | 3 | 75 | | => _errors.AddRange(errors.Select(x => new HttpError(x))); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Initializes a new instance of the <see cref="MultipleHttpException"/> class with a specified error message and a |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | | 81 | | /// <param name="exception">The exception that is the cause of the current exception.</param> |
| | | 82 | | public MultipleHttpException(string message, Exception exception) |
| | 0 | 83 | | : base(message, exception) |
| | | 84 | | { |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Returns a string that represents the current exception, including the main error message and the details of each |
| | | 89 | | /// </summary> |
| | | 90 | | /// <returns>A string representation of the current exception.</returns> |
| | | 91 | | public override string ToString() |
| | | 92 | | { |
| | 3 | 93 | | var errorMessage = new StringBuilder(); |
| | | 94 | | |
| | 3 | 95 | | if (!string.IsNullOrEmpty(Message)) |
| | | 96 | | { |
| | 3 | 97 | | _ = errorMessage.AppendLine(Message); |
| | | 98 | | } |
| | | 99 | | |
| | 18 | 100 | | foreach (var error in Errors) |
| | | 101 | | { |
| | 6 | 102 | | _ = errorMessage.AppendLine(CultureInfo.CurrentCulture, $" - {error.Message}"); |
| | | 103 | | } |
| | | 104 | | |
| | 3 | 105 | | return errorMessage.ToString(); |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Represents an individual HTTP error, encapsulating a message and an optional code that provides specific details abo |
| | | 111 | | /// </summary> |
| | | 112 | | public class HttpError |
| | | 113 | | { |
| | | 114 | | /// <summary> |
| | | 115 | | /// Gets the error message that describes the specific issue that occurred during an HTTP operation. This property p |
| | | 116 | | /// </summary> |
| | | 117 | | public string Message { get; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets an optional code that provides additional information about the specific error that occurred during an HTTP |
| | | 121 | | /// </summary> |
| | | 122 | | public string? Code { get; } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Initializes a new instance of the <see cref="HttpError"/> class with a specified error code and message. This co |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="code">The error code that categorizes the error.</param> |
| | | 128 | | /// <param name="message">The error message that describes the specific issue.</param> |
| | | 129 | | public HttpError(string code, string message) => (Code, Message) = (code, message); |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Initializes a new instance of the <see cref="HttpError"/> class with a specified error message. This constructor |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="message">The error message that describes the specific issue.</param> |
| | | 135 | | public HttpError(string message) => Message = message; |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Returns a string that represents the current error, which is the error message. This method provides a simple wa |
| | | 139 | | /// </summary> |
| | | 140 | | /// <returns>A string that represents the current error.</returns> |
| | | 141 | | public override string ToString() => Message; |
| | | 142 | | } |
| | | 143 | | |