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
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -698,6 +698,13 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "images"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"derive_more",
|
||||
"mime",
|
||||
"reqwest",
|
||||
"utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
|
@ -1458,6 +1465,8 @@ name = "utils"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"derive_more",
|
||||
"reqwest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -6,7 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
utils = { path = "../utils" }
|
||||
utils = { path = "../utils", features = [ "nothuman" ] }
|
||||
actix-web = "4.3.1"
|
||||
derive_more = "0.99.17"
|
||||
mime = "0.3.17"
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
mod error;
|
||||
pub use error::{Error, Result};
|
||||
|
||||
use utils::is_human;
|
||||
use utils::{error::{Error, Result}, is_human};
|
||||
|
||||
use actix_web::{get, http::header, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
|
||||
|
|
|
@ -7,3 +7,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
actix-web = "4.3.1"
|
||||
derive_more = "0.99.17"
|
||||
reqwest = "0.11.18"
|
||||
|
||||
[features]
|
||||
nothuman = []
|
|
@ -1,4 +1,7 @@
|
|||
use actix_web::{HttpResponse, ResponseError};
|
||||
#[cfg(feature = "nothuman")]
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
use actix_web::ResponseError;
|
||||
use derive_more::From;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
|
@ -6,7 +9,8 @@ pub type Result<T> = std::result::Result<T, Error>;
|
|||
|
||||
#[derive(From, Debug)]
|
||||
pub enum Error {
|
||||
TatoebaApi(reqwest::Error),
|
||||
Reqwest(reqwest::Error),
|
||||
#[cfg(feature = "nothuman")]
|
||||
NotHuman { target: String },
|
||||
}
|
||||
|
||||
|
@ -22,12 +26,14 @@ impl ResponseError for Error {
|
|||
use Error::*;
|
||||
match self {
|
||||
// 503 Service Unavailable
|
||||
TatoebaApi(error) => error.status().unwrap_or(StatusCode::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!(
|
||||
|
@ -40,3 +46,4 @@ impl ResponseError for Error {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
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
Reference in a new issue