2 extern crate strum_macros;
4 extern crate smart_default;
19 use std::{fmt, fmt::Display};
22 use tracing_error::SpanTrace;
24 pub type ConnectionId = usize;
26 #[derive(PartialEq, Eq, Hash, Debug, Clone)]
27 pub struct IpAddr(pub String);
29 impl fmt::Display for IpAddr {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 write!(f, "{}", self.0)
36 macro_rules! location_info {
39 "None value at {}:{}, column {}",
47 #[derive(Debug, Error)]
48 #[error("{{\"error\":\"{message}\"}}")]
54 pub fn err_plain(msg: &str) -> Self {
56 message: msg.to_string(),
59 pub fn err<E: Display>(msg: &str, original_error: E) -> Self {
60 warn!("{}", original_error);
62 message: msg.to_string(),
68 pub struct LemmyError {
69 pub inner: anyhow::Error,
70 pub context: SpanTrace,
73 impl<T> From<T> for LemmyError
75 T: Into<anyhow::Error>,
77 fn from(t: T) -> Self {
80 context: SpanTrace::capture(),
85 impl Display for LemmyError {
86 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
92 impl actix_web::error::ResponseError for LemmyError {
93 fn status_code(&self) -> StatusCode {
94 match self.inner.downcast_ref::<diesel::result::Error>() {
95 Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
96 _ => StatusCode::INTERNAL_SERVER_ERROR,