| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WebApiException.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Net; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Http.Exceptions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents an error returned by a web API. |
| | | 14 | | /// </summary> |
| | | 15 | | public class WebApiException : Exception |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="WebApiException"/> class. |
| | | 19 | | /// </summary> |
| | 0 | 20 | | public WebApiException() |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="WebApiException"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="statusCode">HTTP status code returned by the API.</param> |
| | | 28 | | /// <param name="reasonPhrase">HTTP reason phrase returned by the API.</param> |
| | | 29 | | /// <param name="responseBody">Raw response body.</param> |
| | | 30 | | /// <param name="payload">Parsed response payload when available.</param> |
| | | 31 | | public WebApiException(HttpStatusCode statusCode, string? reasonPhrase, string? responseBody, object? payload = null |
| | 3 | 32 | | : base(BuildMessage(statusCode, reasonPhrase, responseBody, payload)) |
| | | 33 | | { |
| | | 34 | | StatusCode = statusCode; |
| | | 35 | | ReasonPhrase = reasonPhrase; |
| | | 36 | | ResponseBody = responseBody; |
| | | 37 | | Payload = payload; |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the HTTP status code returned by the API. |
| | | 42 | | /// </summary> |
| | | 43 | | public HttpStatusCode StatusCode { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the HTTP reason phrase returned by the API. |
| | | 47 | | /// </summary> |
| | | 48 | | public string? ReasonPhrase { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the raw response body returned by the API. |
| | | 52 | | /// </summary> |
| | | 53 | | public string? ResponseBody { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the parsed response payload when available. |
| | | 57 | | /// </summary> |
| | | 58 | | public object? Payload { get; } |
| | | 59 | | |
| | | 60 | | protected WebApiException(string message) |
| | 0 | 61 | | : base(message) |
| | | 62 | | { |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | protected WebApiException(string message, Exception? innerException) |
| | 0 | 66 | | : base(message, innerException) |
| | | 67 | | { |
| | 0 | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | private static string BuildMessage(HttpStatusCode statusCode, string? reasonPhrase, string? responseBody, object? pa |
| | 3 | 71 | | ? $"HTTP {(int)statusCode} {reasonPhrase}: {responseBody}" |
| | 3 | 72 | | : payload is not null ? $"HTTP {(int)statusCode} {reasonPhrase}: {payload}" : $"HTTP {(int)statusCode} {reasonPh |
| | | 73 | | } |
| | | 74 | | |