2020-07-10 18:15:41 +00:00
|
|
|
#[macro_use]
|
2020-12-21 23:27:42 +00:00
|
|
|
extern crate strum_macros;
|
2021-08-04 21:13:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate smart_default;
|
2020-07-10 18:15:41 +00:00
|
|
|
|
2020-09-14 15:29:50 +00:00
|
|
|
pub mod apub;
|
|
|
|
pub mod email;
|
2020-12-21 23:27:42 +00:00
|
|
|
pub mod rate_limit;
|
2020-09-24 13:53:21 +00:00
|
|
|
pub mod request;
|
2020-07-10 18:15:41 +00:00
|
|
|
pub mod settings;
|
2021-03-01 17:24:11 +00:00
|
|
|
|
2021-09-20 15:46:34 +00:00
|
|
|
pub mod claims;
|
2020-09-14 15:29:50 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
pub mod utils;
|
2021-02-09 18:26:06 +00:00
|
|
|
pub mod version;
|
2020-07-10 18:15:41 +00:00
|
|
|
|
2021-01-21 16:32:19 +00:00
|
|
|
use http::StatusCode;
|
2021-10-13 19:50:21 +00:00
|
|
|
use log::warn;
|
|
|
|
use std::{fmt, fmt::Display};
|
2020-09-03 19:45:12 +00:00
|
|
|
use thiserror::Error;
|
2020-09-01 14:25:34 +00:00
|
|
|
|
|
|
|
pub type ConnectionId = usize;
|
2021-03-18 20:25:21 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
|
|
|
pub struct IpAddr(pub String);
|
|
|
|
|
|
|
|
impl fmt::Display for IpAddr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 18:15:41 +00:00
|
|
|
|
2020-08-11 14:31:05 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! location_info {
|
|
|
|
() => {
|
|
|
|
format!(
|
|
|
|
"None value at {}:{}, column {}",
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[error("{{\"error\":\"{message}\"}}")]
|
2021-02-22 18:04:32 +00:00
|
|
|
pub struct ApiError {
|
2021-10-13 19:50:21 +00:00
|
|
|
message: String,
|
2020-09-03 19:45:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 18:04:32 +00:00
|
|
|
impl ApiError {
|
2021-10-13 19:50:21 +00:00
|
|
|
pub fn err_plain(msg: &str) -> Self {
|
|
|
|
ApiError {
|
|
|
|
message: msg.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn err<E: Display>(msg: &str, original_error: E) -> Self {
|
|
|
|
warn!("{}", original_error);
|
2021-02-22 18:04:32 +00:00
|
|
|
ApiError {
|
2020-09-03 19:45:12 +00:00
|
|
|
message: msg.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:25:34 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LemmyError {
|
2020-09-18 14:37:46 +00:00
|
|
|
pub inner: anyhow::Error,
|
2020-09-01 14:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for LemmyError
|
2020-09-02 23:17:35 +00:00
|
|
|
where
|
|
|
|
T: Into<anyhow::Error>,
|
2020-09-01 14:25:34 +00:00
|
|
|
{
|
|
|
|
fn from(t: T) -> Self {
|
|
|
|
LemmyError { inner: t.into() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:50:21 +00:00
|
|
|
impl Display for LemmyError {
|
2020-09-01 14:25:34 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:32:19 +00:00
|
|
|
impl actix_web::error::ResponseError for LemmyError {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match self.inner.downcast_ref::<diesel::result::Error>() {
|
|
|
|
Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
|
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|