using System;
using System.Net;
namespace Discord.Net
{
///
/// The exception that is thrown if an error occurs while processing an Discord HTTP request.
///
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.
///
/// The HTTP status code returned.
/// The request that was sent prior to the exception.
/// The Discord status code returned.
/// The reason behind the exception.
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;
}
}
}