2 extern crate strum_macros;
4 extern crate smart_default;
20 use std::{fmt, fmt::Display};
23 pub type ConnectionId = usize;
25 #[derive(PartialEq, Eq, Hash, Debug, Clone)]
26 pub struct IpAddr(pub String);
28 impl fmt::Display for IpAddr {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "{}", self.0)
35 macro_rules! location_info {
38 "None value at {}:{}, column {}",
46 #[derive(Debug, Error)]
47 #[error("{{\"error\":\"{message}\"}}")]
53 pub fn err_plain(msg: &str) -> Self {
55 message: msg.to_string(),
58 pub fn err<E: Display>(msg: &str, original_error: E) -> Self {
59 warn!("{}", original_error);
61 message: msg.to_string(),
67 pub struct LemmyError {
68 pub inner: anyhow::Error,
71 impl<T> From<T> for LemmyError
73 T: Into<anyhow::Error>,
75 fn from(t: T) -> Self {
76 LemmyError { inner: t.into() }
80 impl Display for LemmyError {
81 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
86 impl actix_web::error::ResponseError for LemmyError {
87 fn status_code(&self) -> StatusCode {
88 match self.inner.downcast_ref::<diesel::result::Error>() {
89 Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
90 _ => StatusCode::INTERNAL_SERVER_ERROR,