using System; using System.Net; namespace Discord.Net { /// /// Describes an exception that occurred during the processing of Discord HTTP requests. /// public class HttpException : Exception { /// /// Gets the HTTP status code returned by Discord. /// /// /// An /// HTTP status code /// from Discord. /// public HttpStatusCode HttpCode { get; } /// /// Gets the JSON error code returned by Discord. /// /// /// A /// JSON error code /// from Discord, or null if none. /// public int? DiscordCode { get; } /// /// Gets the reason of the exception. /// public string Reason { get; } /// /// Gets the request object used to send the request. /// public IRequest Request { get; } /// /// Initializes a new instance of the class. /// public HttpException(HttpStatusCode httpCode, IRequest request, int? discordCode = null, string reason = null) : base(CreateMessage(httpCode, discordCode, reason)) { HttpCode = httpCode; Request = request; DiscordCode = discordCode; Reason = reason; } private static string CreateMessage(HttpStatusCode httpCode, int? discordCode = null, string reason = null) { string msg; if (discordCode != null && discordCode != 0) { if (reason != null) msg = $"The server responded with error {(int)discordCode}: {reason}"; else msg = $"The server responded with error {(int)discordCode}: {httpCode}"; } else { if (reason != null) msg = $"The server responded with error {(int)httpCode}: {reason}"; else msg = $"The server responded with error {(int)httpCode}: {httpCode}"; } return msg; } } }