chore: send rest of the errors to the catchers

This commit is contained in:
Zomatree
2025-07-02 01:16:16 +01:00
parent cf4fe859bf
commit 46e127ccd2
8 changed files with 27 additions and 3 deletions

View File

@@ -38,6 +38,7 @@ impl<'r> FromRequest<'r> for User {
if let Some(user) = user { if let Some(user) = user {
Outcome::Success(user.clone()) Outcome::Success(user.clone())
} else { } else {
request.local_cache(|| Some(create_error!(DuplicateNonce)));
Outcome::Error((Status::Unauthorized, authifier::Error::InvalidSession)) Outcome::Error((Status::Unauthorized, authifier::Error::InvalidSession))
} }
} }

View File

@@ -113,6 +113,8 @@ impl<'r> FromRequest<'r> for IdempotencyKey {
let idempotency = IdempotencyKey { key }; let idempotency = IdempotencyKey { key };
let mut cache = TOKEN_CACHE.lock().await; let mut cache = TOKEN_CACHE.lock().await;
if cache.get(&idempotency.key).is_some() { if cache.get(&idempotency.key).is_some() {
request.local_cache(|| Some(create_error!(DuplicateNonce)));
return Outcome::Error((Status::Conflict, create_error!(DuplicateNonce))); return Outcome::Error((Status::Conflict, create_error!(DuplicateNonce)));
} }

View File

@@ -69,6 +69,7 @@ impl Error {
ErrorType::InvalidProperty => StatusCode::BAD_REQUEST, ErrorType::InvalidProperty => StatusCode::BAD_REQUEST,
ErrorType::InvalidSession => StatusCode::UNAUTHORIZED, ErrorType::InvalidSession => StatusCode::UNAUTHORIZED,
ErrorType::NotAuthenticated => StatusCode::UNAUTHORIZED, ErrorType::NotAuthenticated => StatusCode::UNAUTHORIZED,
ErrorType::Conflict => StatusCode::CONFLICT,
ErrorType::DuplicateNonce => StatusCode::CONFLICT, ErrorType::DuplicateNonce => StatusCode::CONFLICT,
ErrorType::VosoUnavailable => StatusCode::BAD_REQUEST, ErrorType::VosoUnavailable => StatusCode::BAD_REQUEST,
ErrorType::NotFound => StatusCode::NOT_FOUND, ErrorType::NotFound => StatusCode::NOT_FOUND,

View File

@@ -152,6 +152,7 @@ pub enum ErrorType {
InvalidSession, InvalidSession,
InvalidFlagValue, InvalidFlagValue,
NotAuthenticated, NotAuthenticated,
Conflict,
DuplicateNonce, DuplicateNonce,
NotFound, NotFound,
NoEffect, NoEffect,
@@ -180,7 +181,7 @@ pub enum ErrorType {
// ? Feature flag disabled in the config // ? Feature flag disabled in the config
FeatureDisabled { FeatureDisabled {
feature: String, feature: String,
}, }
} }
#[macro_export] #[macro_export]

View File

@@ -76,6 +76,7 @@ impl Error {
ErrorType::InvalidProperty => Status::BadRequest, ErrorType::InvalidProperty => Status::BadRequest,
ErrorType::InvalidSession => Status::Unauthorized, ErrorType::InvalidSession => Status::Unauthorized,
ErrorType::NotAuthenticated => Status::Unauthorized, ErrorType::NotAuthenticated => Status::Unauthorized,
ErrorType::Conflict => Status::Conflict,
ErrorType::DuplicateNonce => Status::Conflict, ErrorType::DuplicateNonce => Status::Conflict,
ErrorType::VosoUnavailable => Status::BadRequest, ErrorType::VosoUnavailable => Status::BadRequest,
ErrorType::NotFound => Status::NotFound, ErrorType::NotFound => Status::NotFound,

View File

@@ -636,6 +636,8 @@ impl<'r> FromRequest<'r> for EventHeader<'r> {
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> { async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
let headers = request.headers(); let headers = request.headers();
let Some(event) = headers.get_one("X-GitHub-Event") else { let Some(event) = headers.get_one("X-GitHub-Event") else {
request.local_cache(|| Some(create_error!(InvalidOperation)));
return rocket::request::Outcome::Error(( return rocket::request::Outcome::Error((
Status::BadRequest, Status::BadRequest,
create_error!(InvalidOperation), create_error!(InvalidOperation),

View File

@@ -14,6 +14,22 @@ pub fn unprocessable_entity(req: &Request) -> Result<()> {
} }
} }
#[catch(401)]
pub fn unauthorized(req: &Request) -> Result<()> {
match req.local_cache(|| None::<Error>) {
Some(e) => Err(e.clone()),
None => Err(create_error!(NotAuthenticated))
}
}
#[catch(409)]
pub fn conflict(req: &Request) -> Result<()> {
match req.local_cache(|| None::<Error>) {
Some(e) => Err(e.clone()),
None => Err(create_error!(Conflict))
}
}
pub fn all_catchers() -> Vec<Catcher> { pub fn all_catchers() -> Vec<Catcher> {
catchers![not_found, unprocessable_entity] catchers![not_found, unprocessable_entity, unauthorized, conflict]
} }

View File

@@ -3,12 +3,12 @@ use std::hash::Hasher;
use std::ops::Add; use std::ops::Add;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::util::json::Json;
use authifier::models::Session; use authifier::models::Session;
use rocket::fairing::{Fairing, Info, Kind}; use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::uri::Origin; use rocket::http::uri::Origin;
use rocket::http::{Method, Status}; use rocket::http::{Method, Status};
use rocket::request::{FromRequest, Outcome}; use rocket::request::{FromRequest, Outcome};
use crate::util::json::Json;
use rocket::{Data, Request, Response}; use rocket::{Data, Request, Response};
use revolt_rocket_okapi::gen::OpenApiGenerator; use revolt_rocket_okapi::gen::OpenApiGenerator;