Verified Commit c011a201 authored by Mathias B.'s avatar Mathias B.
Browse files

Utilisation directe du code HTTP renvoyé par la librairie HTTP de Serenity

parent a91d7caa
use actix_web::{delete, get, HttpResponse, post, put, web};
use serenity::http::error::{Error as SerenityHTTPError, Error};
use crate::rpc_server::DiscordClient;
use crate::rpc_server::utilities::ClientError;
......
use actix_web::http::StatusCode;
use serenity::{Error as SerenityError, Error};
use serenity::Error as SerenityError;
use serenity::http::HttpError as SerenityHTTPError;
#[derive(thiserror::Error, Debug)]
pub enum ClientError {
#[error("Discord has returned HTTP Forbidden")]
DiscordForbidden,
#[error("Serenity has returned an unhandled error")]
DiscordUnknown,
#[error("Discord has rate-limited us")]
DiscordRateLimited,
#[error("Discord returned HTTP code {0}")]
DirectSerenityHTTPCode(StatusCode),
#[error("Content not found")]
NotFound,
#[error("An unknown error has occurred")]
......@@ -20,11 +16,10 @@ pub enum ClientError {
impl actix_web::error::ResponseError for ClientError {
fn status_code(&self) -> StatusCode {
match self {
ClientError::DiscordForbidden => StatusCode::FORBIDDEN,
ClientError::DirectSerenityHTTPCode(code) => *code,
ClientError::NotFound => StatusCode::NOT_FOUND,
ClientError::Unknown
| ClientError::DiscordUnknown => StatusCode::INTERNAL_SERVER_ERROR,
ClientError::DiscordRateLimited => StatusCode::TOO_MANY_REQUESTS
ClientError::Unknown => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
......@@ -32,12 +27,12 @@ impl actix_web::error::ResponseError for ClientError {
impl From<SerenityError> for ClientError {
fn from(serenity_error: SerenityError) -> Self {
match serenity_error {
Error::Http(http_error) => {
SerenityError::Http(http_error) => {
Self::from(http_error.as_ref())
}
_ => {
tracing::warn!("Tried to convert an unknown error into ClientError: {:#?}", serenity_error);
Self::DiscordUnknown
Self::Unknown
}
}
}
......@@ -46,19 +41,11 @@ impl From<SerenityError> for ClientError {
impl From<&SerenityHTTPError> for ClientError {
fn from(http_error: &SerenityHTTPError) -> Self {
if http_error.is_unsuccessful_request() {
// TODO: Intégrer le code HTTP directement dans l'erreur UserError
match http_error.status_code().unwrap() {
serenity::http::StatusCode::FORBIDDEN => Self::DiscordForbidden,
serenity::http::StatusCode::NOT_FOUND => Self::NotFound,
serenity::http::StatusCode::TOO_MANY_REQUESTS => Self::DiscordRateLimited,
_ => {
tracing::warn!("Unhandled HTTP error code {:?}", http_error.status_code().unwrap());
Self::DiscordUnknown
}
}
let status_code = http_error.status_code().unwrap();
Self::DirectSerenityHTTPCode(status_code)
} else {
tracing::warn!("Tried to convert an unknown HTTP error into ClientError: {:#?}", http_error);
Self::DiscordUnknown
Self::Unknown
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment