Fix formatting

error-expose
SleeplessOne1917 2024-02-09 18:16:42 -05:00
parent b163f7612f
commit 64e7ca406c
1 changed files with 94 additions and 95 deletions

View File

@ -170,113 +170,112 @@ pub enum LemmyErrorType {
cfg_if! { cfg_if! {
if #[cfg(feature = "error")] { if #[cfg(feature = "error")] {
use tracing_error::SpanTrace; use tracing_error::SpanTrace;
use std::fmt; use std::fmt;
pub type LemmyResult<T> = Result<T, LemmyError>; pub type LemmyResult<T> = Result<T, LemmyError>;
pub struct LemmyError { pub struct LemmyError {
pub error_type: LemmyErrorType, pub error_type: LemmyErrorType,
pub inner: anyhow::Error, pub inner: anyhow::Error,
pub context: SpanTrace, 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<T> From<T> for LemmyError
where
T: Into<anyhow::Error>,
{
fn from(t: T) -> Self {
let cause = t.into();
LemmyError {
error_type: LemmyErrorType::Unknown(format!("{}", &cause)),
inner: cause,
context: SpanTrace::capture(),
} }
}
}
impl Debug for LemmyError { /// Maximum number of items in an array passed as API parameter. See [[LemmyErrorType::TooManyItems]]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { pub const MAX_API_PARAM_ELEMENTS: usize = 10_000;
f.debug_struct("LemmyError")
.field("message", &self.error_type)
.field("inner", &self.inner)
.field("context", &self.context)
.finish()
}
}
impl fmt::Display for LemmyError { impl<T> From<T> for LemmyError
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { where
write!(f, "{}: ", &self.error_type)?; T: Into<anyhow::Error>,
// print anyhow including trace {
// https://docs.rs/anyhow/latest/anyhow/struct.Error.html#display-representations fn from(t: T) -> Self {
// this will print the anyhow trace (only if it exists) let cause = t.into();
// and if RUST_BACKTRACE=1, also a full backtrace LemmyError {
writeln!(f, "{:?}", self.inner)?; error_type: LemmyErrorType::Unknown(format!("{}", &cause)),
fmt::Display::fmt(&self.context, f) inner: cause,
} context: SpanTrace::capture(),
} }
}
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::<diesel::result::Error>() {
Some(diesel::result::Error::NotFound) => http::StatusCode::NOT_FOUND, impl Debug for LemmyError {
_ => http::StatusCode::BAD_REQUEST, 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 { impl fmt::Display for LemmyError {
actix_web::HttpResponse::build(self.status_code()).json(&self.error_type) 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
impl From<LemmyErrorType> for LemmyError { // this will print the anyhow trace (only if it exists)
fn from(error_type: LemmyErrorType) -> Self { // and if RUST_BACKTRACE=1, also a full backtrace
let inner = anyhow::anyhow!("{}", error_type); writeln!(f, "{:?}", self.inner)?;
LemmyError { fmt::Display::fmt(&self.context, f)
error_type, }
inner,
context: SpanTrace::capture(),
} }
}
}
pub trait LemmyErrorExt<T, E: Into<anyhow::Error>> { impl actix_web::error::ResponseError for LemmyError {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>; fn status_code(&self) -> http::StatusCode {
} if self.error_type == LemmyErrorType::IncorrectLogin {
return http::StatusCode::UNAUTHORIZED;
}
match self.inner.downcast_ref::<diesel::result::Error>() {
Some(diesel::result::Error::NotFound) => http::StatusCode::NOT_FOUND,
_ => http::StatusCode::BAD_REQUEST,
}
}
impl<T, E: Into<anyhow::Error>> LemmyErrorExt<T, E> for Result<T, E> { fn error_response(&self) -> actix_web::HttpResponse {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> { actix_web::HttpResponse::build(self.status_code()).json(&self.error_type)
self.map_err(|error| LemmyError { }
error_type, }
inner: error.into(),
context: SpanTrace::capture(),
})
}
}
pub trait LemmyErrorExt2<T> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>;
fn into_anyhow(self) -> Result<T, anyhow::Error>;
}
impl<T> LemmyErrorExt2<T> for Result<T, LemmyError> { impl From<LemmyErrorType> for LemmyError {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> { fn from(error_type: LemmyErrorType) -> Self {
self.map_err(|mut e| { let inner = anyhow::anyhow!("{}", error_type);
e.error_type = error_type; LemmyError {
e error_type,
}) inner,
} context: SpanTrace::capture(),
// 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<T, anyhow::Error> { }
self.map_err(|e| e.inner) }
}
}
pub trait LemmyErrorExt<T, E: Into<anyhow::Error>> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>;
}
impl<T, E: Into<anyhow::Error>> LemmyErrorExt<T, E> for Result<T, E> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> {
self.map_err(|error| LemmyError {
error_type,
inner: error.into(),
context: SpanTrace::capture(),
})
}
}
pub trait LemmyErrorExt2<T> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>;
fn into_anyhow(self) -> Result<T, anyhow::Error>;
}
impl<T> LemmyErrorExt2<T> for Result<T, LemmyError> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> {
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<T, anyhow::Error> {
self.map_err(|e| e.inner)
}
}
} }
} }