generated from ElnuDev/rust-project
Move error handling into utils
This commit is contained in:
parent
7c043e8357
commit
ef562fe656
7 changed files with 52 additions and 28 deletions
|
@ -7,3 +7,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
actix-web = "4.3.1"
|
||||
derive_more = "0.99.17"
|
||||
reqwest = "0.11.18"
|
||||
|
||||
[features]
|
||||
nothuman = []
|
49
utils/src/error.rs
Normal file
49
utils/src/error.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
#[cfg(feature = "nothuman")]
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
use actix_web::ResponseError;
|
||||
use derive_more::From;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(From, Debug)]
|
||||
pub enum Error {
|
||||
Reqwest(reqwest::Error),
|
||||
#[cfg(feature = "nothuman")]
|
||||
NotHuman { target: String },
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn status_code(&self) -> reqwest::StatusCode {
|
||||
use reqwest::StatusCode;
|
||||
use Error::*;
|
||||
match self {
|
||||
// 503 Service Unavailable
|
||||
Reqwest(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE),
|
||||
// 403 Forbidden
|
||||
#[cfg(feature = "nothuman")]
|
||||
NotHuman { .. } => StatusCode::FORBIDDEN,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nothuman")]
|
||||
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
|
||||
HttpResponse::build(self.status_code()).body(match self {
|
||||
Self::NotHuman { target } => format!(
|
||||
"It looks like you're accessing the Tatoeba API proxy from a script!\n\
|
||||
Tatoeba CORS restrictions do not apply outside of browsers, so please access the API directly:\n\
|
||||
{target}\n\
|
||||
If you feel this is mistake, please open an issue:\n\
|
||||
https://codeberg.org/ElnuDev/tatoeba-api-rs"),
|
||||
_ => self.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
21
utils/src/is_human.rs
Normal file
21
utils/src/is_human.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use actix_web::{http::header, HttpRequest};
|
||||
|
||||
pub fn is_human(request: &HttpRequest) -> bool {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
|
||||
const HUMANS: &[&str] = &[
|
||||
"Firefox", "Chrome", // Chrome and Chromium browsers
|
||||
"Opera", // Old Presto-based Opera
|
||||
"Mobile", // Safari
|
||||
"Trident", // Internet Explorer
|
||||
];
|
||||
request
|
||||
.headers()
|
||||
.get(header::USER_AGENT)
|
||||
.and_then(|header| header.to_str().ok())
|
||||
.map(|ua| {
|
||||
HUMANS
|
||||
.iter()
|
||||
.any(|&human| ua.contains(human))
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
|
@ -1,21 +1,6 @@
|
|||
use actix_web::{http::header, HttpRequest};
|
||||
pub mod error;
|
||||
|
||||
pub fn is_human(request: &HttpRequest) -> bool {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
|
||||
const HUMANS: &[&str] = &[
|
||||
"Firefox", "Chrome", // Chrome and Chromium browsers
|
||||
"Opera", // Old Presto-based Opera
|
||||
"Mobile", // Safari
|
||||
"Trident", // Internet Explorer
|
||||
];
|
||||
request
|
||||
.headers()
|
||||
.get(header::USER_AGENT)
|
||||
.and_then(|header| header.to_str().ok())
|
||||
.map(|ua| {
|
||||
HUMANS
|
||||
.iter()
|
||||
.any(|&human| ua.contains(human))
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
#[cfg(feature = "nothuman")]
|
||||
mod is_human;
|
||||
#[cfg(feature = "nothuman")]
|
||||
pub use is_human::is_human;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue