2 extern crate lazy_static;
4 extern crate strum_macros;
6 extern crate smart_default;
22 use std::{fmt, fmt::Display};
25 pub type ConnectionId = usize;
27 #[derive(PartialEq, Eq, Hash, Debug, Clone)]
28 pub struct IpAddr(pub String);
30 impl fmt::Display for IpAddr {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 write!(f, "{}", self.0)
37 macro_rules! location_info {
40 "None value at {}:{}, column {}",
48 #[derive(Debug, Error)]
49 #[error("{{\"error\":\"{message}\"}}")]
55 pub fn err_plain(msg: &str) -> Self {
57 message: msg.to_string(),
60 pub fn err<E: Display>(msg: &str, original_error: E) -> Self {
61 warn!("{}", original_error);
63 message: msg.to_string(),
69 pub struct LemmyError {
70 pub inner: anyhow::Error,
73 impl<T> From<T> for LemmyError
75 T: Into<anyhow::Error>,
77 fn from(t: T) -> Self {
78 LemmyError { inner: t.into() }
82 impl Display for LemmyError {
83 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
88 impl actix_web::error::ResponseError for LemmyError {
89 fn status_code(&self) -> StatusCode {
90 match self.inner.downcast_ref::<diesel::result::Error>() {
91 Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
92 _ => StatusCode::INTERNAL_SERVER_ERROR,