|
| 1 | +using System.Net; |
| 2 | + |
| 3 | +namespace ModularPipelines.Exceptions; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Exception thrown when an HTTP request returns a non-success status code. |
| 7 | +/// Provides detailed information about the failed response including status code, reason phrase, and response content. |
| 8 | +/// </summary> |
| 9 | +public class HttpResponseException : PipelineException |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Gets the HTTP status code of the failed response. |
| 13 | + /// </summary> |
| 14 | + public HttpStatusCode StatusCode { get; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Gets the reason phrase from the failed response. |
| 18 | + /// </summary> |
| 19 | + public string? ReasonPhrase { get; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Gets the content of the failed response, if available. |
| 23 | + /// </summary> |
| 24 | + public string? ResponseContent { get; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets the request URI that produced the failed response. |
| 28 | + /// </summary> |
| 29 | + public Uri? RequestUri { get; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="HttpResponseException"/> class. |
| 33 | + /// </summary> |
| 34 | + /// <param name="statusCode">The HTTP status code.</param> |
| 35 | + /// <param name="reasonPhrase">The reason phrase from the response.</param> |
| 36 | + /// <param name="responseContent">The content of the response.</param> |
| 37 | + /// <param name="requestUri">The request URI.</param> |
| 38 | + public HttpResponseException(HttpStatusCode statusCode, string? reasonPhrase, string? responseContent, Uri? requestUri) |
| 39 | + : base(FormatMessage(statusCode, reasonPhrase, responseContent, requestUri)) |
| 40 | + { |
| 41 | + StatusCode = statusCode; |
| 42 | + ReasonPhrase = reasonPhrase; |
| 43 | + ResponseContent = responseContent; |
| 44 | + RequestUri = requestUri; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="HttpResponseException"/> class with an inner exception. |
| 49 | + /// </summary> |
| 50 | + /// <param name="statusCode">The HTTP status code.</param> |
| 51 | + /// <param name="reasonPhrase">The reason phrase from the response.</param> |
| 52 | + /// <param name="responseContent">The content of the response.</param> |
| 53 | + /// <param name="requestUri">The request URI.</param> |
| 54 | + /// <param name="innerException">The inner exception.</param> |
| 55 | + public HttpResponseException(HttpStatusCode statusCode, string? reasonPhrase, string? responseContent, Uri? requestUri, Exception? innerException) |
| 56 | + : base(FormatMessage(statusCode, reasonPhrase, responseContent, requestUri), innerException) |
| 57 | + { |
| 58 | + StatusCode = statusCode; |
| 59 | + ReasonPhrase = reasonPhrase; |
| 60 | + ResponseContent = responseContent; |
| 61 | + RequestUri = requestUri; |
| 62 | + } |
| 63 | + |
| 64 | + private static string FormatMessage(HttpStatusCode statusCode, string? reasonPhrase, string? responseContent, Uri? requestUri) |
| 65 | + { |
| 66 | + var message = $"HTTP request failed with status code {(int)statusCode} ({statusCode})"; |
| 67 | + |
| 68 | + if (!string.IsNullOrWhiteSpace(reasonPhrase)) |
| 69 | + { |
| 70 | + message += $": {reasonPhrase}"; |
| 71 | + } |
| 72 | + |
| 73 | + if (requestUri is not null) |
| 74 | + { |
| 75 | + message += $"\nRequest URI: {requestUri}"; |
| 76 | + } |
| 77 | + |
| 78 | + if (!string.IsNullOrWhiteSpace(responseContent)) |
| 79 | + { |
| 80 | + // Truncate very long responses to avoid excessive exception messages |
| 81 | + const int maxContentLength = 2000; |
| 82 | + var truncatedContent = responseContent.Length > maxContentLength |
| 83 | + ? responseContent[..maxContentLength] + "... (truncated)" |
| 84 | + : responseContent; |
| 85 | + message += $"\nResponse content: {truncatedContent}"; |
| 86 | + } |
| 87 | + |
| 88 | + return message; |
| 89 | + } |
| 90 | +} |
0 commit comments