You've already forked AstralRinth
forked from didirus/AstralRinth
Upgrade to Actix V2, bump SQLX version, code cleanup, intergrate ratelimiter (#288)
* Upgrade to Actix V2, bump SQLX version, code cleanup, intergrate ratelimiter * Add pack file path validation * Fix compilation error caused by incorrect merge
This commit is contained in:
45
src/ratelimit/errors.rs
Normal file
45
src/ratelimit/errors.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! Errors that can occur during middleware processing stage
|
||||
use actix_web::ResponseError;
|
||||
use log::*;
|
||||
use thiserror::Error;
|
||||
|
||||
/// Custom error type. Useful for logging and debugging different kinds of errors.
|
||||
/// This type can be converted to Actix Error, which defaults to
|
||||
/// InternalServerError
|
||||
///
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ARError {
|
||||
/// Read/Write error on store
|
||||
#[error("read/write operatiion failed: {0}")]
|
||||
ReadWriteError(String),
|
||||
|
||||
/// Identifier error
|
||||
#[error("client identification failed")]
|
||||
IdentificationError,
|
||||
/// Limited Error
|
||||
#[error("You are being ratelimited. Please wait {reset} seconds. {remaining}/{max_requests} remaining.")]
|
||||
LimitedError {
|
||||
max_requests: usize,
|
||||
remaining: usize,
|
||||
reset: u64,
|
||||
},
|
||||
}
|
||||
|
||||
impl ResponseError for ARError {
|
||||
fn error_response(&self) -> actix_web::web::HttpResponse {
|
||||
match self {
|
||||
Self::LimitedError {
|
||||
max_requests,
|
||||
remaining,
|
||||
reset,
|
||||
} => {
|
||||
let mut response = actix_web::web::HttpResponse::TooManyRequests();
|
||||
response.insert_header(("x-ratelimit-limit", max_requests.to_string()));
|
||||
response.insert_header(("x-ratelimit-remaining", remaining.to_string()));
|
||||
response.insert_header(("x-ratelimit-reset", reset.to_string()));
|
||||
response.body(self.to_string())
|
||||
}
|
||||
_ => actix_web::web::HttpResponse::build(self.status_code()).body(self.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user