2020-07-10 12:15:41 -06:00
|
|
|
#[macro_use]
|
2020-12-21 16:27:42 -07:00
|
|
|
extern crate strum_macros;
|
2021-08-04 15:13:51 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate smart_default;
|
2020-07-10 12:15:41 -06:00
|
|
|
|
2020-09-14 09:29:50 -06:00
|
|
|
pub mod apub;
|
|
|
|
pub mod email;
|
2020-12-21 16:27:42 -07:00
|
|
|
pub mod rate_limit;
|
2020-07-10 12:15:41 -06:00
|
|
|
pub mod settings;
|
2021-03-01 10:24:11 -07:00
|
|
|
|
2021-09-20 09:46:34 -06:00
|
|
|
pub mod claims;
|
2022-05-03 11:44:13 -06:00
|
|
|
pub mod request;
|
2020-09-14 09:29:50 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
pub mod utils;
|
2021-02-09 11:26:06 -07:00
|
|
|
pub mod version;
|
2020-07-10 12:15:41 -06:00
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
use actix_web::HttpResponse;
|
2021-01-21 09:32:19 -07:00
|
|
|
use http::StatusCode;
|
2022-03-24 10:33:23 -06:00
|
|
|
use std::{fmt, fmt::Display, time::Duration};
|
2021-11-23 05:16:47 -07:00
|
|
|
use tracing_error::SpanTrace;
|
2020-09-01 08:25:34 -06:00
|
|
|
|
|
|
|
pub type ConnectionId = usize;
|
2021-03-18 14:25:21 -06:00
|
|
|
|
2022-03-24 10:33:23 -06:00
|
|
|
pub const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
|
2021-03-18 14:25:21 -06: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 12:15:41 -06:00
|
|
|
|
2020-08-11 08:31:05 -06:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! location_info {
|
|
|
|
() => {
|
|
|
|
format!(
|
|
|
|
"None value at {}:{}, column {}",
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct ApiError {
|
2022-04-19 04:48:59 -06:00
|
|
|
error: String,
|
2021-12-06 07:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LemmyError {
|
2022-04-19 04:48:59 -06:00
|
|
|
pub message: Option<String>,
|
2021-12-06 07:54:47 -07:00
|
|
|
pub inner: anyhow::Error,
|
|
|
|
pub context: SpanTrace,
|
2020-09-03 13:45:12 -06:00
|
|
|
}
|
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
impl LemmyError {
|
2022-03-16 14:11:49 -06:00
|
|
|
/// Create LemmyError from a message, including stack trace
|
2022-04-19 04:48:59 -06:00
|
|
|
pub fn from_message(message: &str) -> Self {
|
2021-12-06 07:54:47 -07:00
|
|
|
let inner = anyhow::anyhow!("{}", message);
|
|
|
|
LemmyError {
|
2022-04-19 04:48:59 -06:00
|
|
|
message: Some(message.into()),
|
2021-12-06 07:54:47 -07:00
|
|
|
inner,
|
|
|
|
context: SpanTrace::capture(),
|
2021-10-13 13:50:21 -06:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 14:11:49 -06:00
|
|
|
|
|
|
|
/// Create a LemmyError from error and message, including stack trace
|
2022-04-19 04:48:59 -06:00
|
|
|
pub fn from_error_message<E>(error: E, message: &str) -> Self
|
2022-03-16 14:11:49 -06:00
|
|
|
where
|
|
|
|
E: Into<anyhow::Error>,
|
|
|
|
{
|
|
|
|
LemmyError {
|
2022-04-19 04:48:59 -06:00
|
|
|
message: Some(message.into()),
|
2022-03-16 14:11:49 -06:00
|
|
|
inner: error.into(),
|
|
|
|
context: SpanTrace::capture(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add message to existing LemmyError (or overwrite existing error)
|
2022-04-19 04:48:59 -06:00
|
|
|
pub fn with_message(self, message: &str) -> Self {
|
2021-12-06 07:54:47 -07:00
|
|
|
LemmyError {
|
2022-04-19 04:48:59 -06:00
|
|
|
message: Some(message.into()),
|
2021-12-06 07:54:47 -07:00
|
|
|
..self
|
2020-09-03 13:45:12 -06:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 14:11:49 -06:00
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
pub fn to_json(&self) -> Result<String, Self> {
|
2022-04-19 04:48:59 -06:00
|
|
|
let api_error = match &self.message {
|
|
|
|
Some(error) => ApiError {
|
|
|
|
error: error.into(),
|
|
|
|
},
|
|
|
|
None => ApiError {
|
|
|
|
error: "Unknown".into(),
|
|
|
|
},
|
2021-12-06 07:54:47 -07:00
|
|
|
};
|
2020-09-03 13:45:12 -06:00
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
Ok(serde_json::to_string(&api_error)?)
|
|
|
|
}
|
2020-09-01 08:25:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for LemmyError
|
2020-09-02 17:17:35 -06:00
|
|
|
where
|
|
|
|
T: Into<anyhow::Error>,
|
2020-09-01 08:25:34 -06:00
|
|
|
{
|
|
|
|
fn from(t: T) -> Self {
|
2021-11-23 05:16:47 -07:00
|
|
|
LemmyError {
|
2021-12-06 07:54:47 -07:00
|
|
|
message: None,
|
2021-11-23 05:16:47 -07:00
|
|
|
inner: t.into(),
|
|
|
|
context: SpanTrace::capture(),
|
|
|
|
}
|
2020-09-01 08:25:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 07:54:47 -07:00
|
|
|
impl std::fmt::Debug for LemmyError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("LemmyError")
|
|
|
|
.field("message", &self.message)
|
|
|
|
.field("inner", &self.inner)
|
|
|
|
.field("context", &"SpanTrace")
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 13:50:21 -06:00
|
|
|
impl Display for LemmyError {
|
2020-09-01 08:25:34 -06:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2022-04-19 04:48:59 -06:00
|
|
|
if let Some(message) = &self.message {
|
2021-12-06 07:54:47 -07:00
|
|
|
write!(f, "{}: ", message)?;
|
|
|
|
}
|
|
|
|
writeln!(f, "{}", self.inner)?;
|
2021-11-23 05:16:47 -07:00
|
|
|
self.context.fmt(f)
|
2020-09-01 08:25:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:32:19 -07: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,
|
2021-12-06 07:54:47 -07:00
|
|
|
_ => StatusCode::BAD_REQUEST,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
if let Some(message) = &self.message {
|
2022-04-19 04:48:59 -06:00
|
|
|
HttpResponse::build(self.status_code()).json(ApiError {
|
|
|
|
error: message.into(),
|
|
|
|
})
|
2021-12-06 07:54:47 -07:00
|
|
|
} else {
|
|
|
|
HttpResponse::build(self.status_code())
|
|
|
|
.content_type("text/plain")
|
|
|
|
.body(self.inner.to_string())
|
2021-01-21 09:32:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|