From 64e7ca406c046b2512d4bd0b7c5efbf2f33a279a Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Fri, 9 Feb 2024 18:16:42 -0500 Subject: [PATCH] Fix formatting --- crates/utils/src/error.rs | 189 +++++++++++++++++++------------------- 1 file changed, 94 insertions(+), 95 deletions(-) diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 8c2d485cf..31e0c3ddd 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -170,113 +170,112 @@ pub enum LemmyErrorType { cfg_if! { if #[cfg(feature = "error")] { -use tracing_error::SpanTrace; -use std::fmt; - pub type LemmyResult = Result; + use tracing_error::SpanTrace; + use std::fmt; + pub type LemmyResult = Result; -pub struct LemmyError { - pub error_type: LemmyErrorType, - pub inner: anyhow::Error, - pub context: SpanTrace, -} - -/// Maximum number of items in an array passed as API parameter. See [[LemmyErrorType::TooManyItems]] -pub const MAX_API_PARAM_ELEMENTS: usize = 10_000; - -impl From for LemmyError -where - T: Into, -{ - fn from(t: T) -> Self { - let cause = t.into(); - LemmyError { - error_type: LemmyErrorType::Unknown(format!("{}", &cause)), - inner: cause, - context: SpanTrace::capture(), + pub struct LemmyError { + pub error_type: LemmyErrorType, + pub inner: anyhow::Error, + pub context: SpanTrace, } - } -} -impl Debug for LemmyError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("LemmyError") - .field("message", &self.error_type) - .field("inner", &self.inner) - .field("context", &self.context) - .finish() - } -} + /// Maximum number of items in an array passed as API parameter. See [[LemmyErrorType::TooManyItems]] + pub const MAX_API_PARAM_ELEMENTS: usize = 10_000; -impl fmt::Display for LemmyError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}: ", &self.error_type)?; - // print anyhow including trace - // https://docs.rs/anyhow/latest/anyhow/struct.Error.html#display-representations - // this will print the anyhow trace (only if it exists) - // and if RUST_BACKTRACE=1, also a full backtrace - writeln!(f, "{:?}", self.inner)?; - fmt::Display::fmt(&self.context, f) - } -} - -impl actix_web::error::ResponseError for LemmyError { - fn status_code(&self) -> http::StatusCode { - if self.error_type == LemmyErrorType::IncorrectLogin { - return http::StatusCode::UNAUTHORIZED; + impl From for LemmyError + where + T: Into, + { + fn from(t: T) -> Self { + let cause = t.into(); + LemmyError { + error_type: LemmyErrorType::Unknown(format!("{}", &cause)), + inner: cause, + context: SpanTrace::capture(), + } + } } - match self.inner.downcast_ref::() { - Some(diesel::result::Error::NotFound) => http::StatusCode::NOT_FOUND, - _ => http::StatusCode::BAD_REQUEST, + + impl Debug for LemmyError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("LemmyError") + .field("message", &self.error_type) + .field("inner", &self.inner) + .field("context", &self.context) + .finish() + } } - } - fn error_response(&self) -> actix_web::HttpResponse { - actix_web::HttpResponse::build(self.status_code()).json(&self.error_type) - } -} - -impl From for LemmyError { - fn from(error_type: LemmyErrorType) -> Self { - let inner = anyhow::anyhow!("{}", error_type); - LemmyError { - error_type, - inner, - context: SpanTrace::capture(), + impl fmt::Display for LemmyError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}: ", &self.error_type)?; + // print anyhow including trace + // https://docs.rs/anyhow/latest/anyhow/struct.Error.html#display-representations + // this will print the anyhow trace (only if it exists) + // and if RUST_BACKTRACE=1, also a full backtrace + writeln!(f, "{:?}", self.inner)?; + fmt::Display::fmt(&self.context, f) + } } - } -} -pub trait LemmyErrorExt> { - fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result; -} + impl actix_web::error::ResponseError for LemmyError { + fn status_code(&self) -> http::StatusCode { + if self.error_type == LemmyErrorType::IncorrectLogin { + return http::StatusCode::UNAUTHORIZED; + } + match self.inner.downcast_ref::() { + Some(diesel::result::Error::NotFound) => http::StatusCode::NOT_FOUND, + _ => http::StatusCode::BAD_REQUEST, + } + } -impl> LemmyErrorExt for Result { - fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result { - self.map_err(|error| LemmyError { - error_type, - inner: error.into(), - context: SpanTrace::capture(), - }) - } -} -pub trait LemmyErrorExt2 { - fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result; - fn into_anyhow(self) -> Result; -} + fn error_response(&self) -> actix_web::HttpResponse { + actix_web::HttpResponse::build(self.status_code()).json(&self.error_type) + } + } -impl LemmyErrorExt2 for Result { - fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result { - self.map_err(|mut e| { - e.error_type = error_type; - e - }) - } - // this function can't be an impl From or similar because it would conflict with one of the other broad Into<> implementations - fn into_anyhow(self) -> Result { - self.map_err(|e| e.inner) - } -} + impl From for LemmyError { + fn from(error_type: LemmyErrorType) -> Self { + let inner = anyhow::anyhow!("{}", error_type); + LemmyError { + error_type, + inner, + context: SpanTrace::capture(), + } + } + } + pub trait LemmyErrorExt> { + fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result; + } + + impl> LemmyErrorExt for Result { + fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result { + self.map_err(|error| LemmyError { + error_type, + inner: error.into(), + context: SpanTrace::capture(), + }) + } + } + pub trait LemmyErrorExt2 { + fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result; + fn into_anyhow(self) -> Result; + } + + impl LemmyErrorExt2 for Result { + fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result { + self.map_err(|mut e| { + e.error_type = error_type; + e + }) + } + // this function can't be an impl From or similar because it would conflict with one of the other broad Into<> implementations + fn into_anyhow(self) -> Result { + self.map_err(|e| e.inner) + } + } } }