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,23 +170,23 @@ 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]] /// Maximum number of items in an array passed as API parameter. See [[LemmyErrorType::TooManyItems]]
pub const MAX_API_PARAM_ELEMENTS: usize = 10_000; pub const MAX_API_PARAM_ELEMENTS: usize = 10_000;
impl<T> From<T> for LemmyError impl<T> From<T> for LemmyError
where where
T: Into<anyhow::Error>, T: Into<anyhow::Error>,
{ {
fn from(t: T) -> Self { fn from(t: T) -> Self {
let cause = t.into(); let cause = t.into();
LemmyError { LemmyError {
@ -195,9 +195,9 @@ where
context: SpanTrace::capture(), context: SpanTrace::capture(),
} }
} }
} }
impl Debug for LemmyError { impl Debug for LemmyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LemmyError") f.debug_struct("LemmyError")
.field("message", &self.error_type) .field("message", &self.error_type)
@ -205,9 +205,9 @@ impl Debug for LemmyError {
.field("context", &self.context) .field("context", &self.context)
.finish() .finish()
} }
} }
impl fmt::Display for LemmyError { impl fmt::Display for LemmyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: ", &self.error_type)?; write!(f, "{}: ", &self.error_type)?;
// print anyhow including trace // print anyhow including trace
@ -217,9 +217,9 @@ impl fmt::Display for LemmyError {
writeln!(f, "{:?}", self.inner)?; writeln!(f, "{:?}", self.inner)?;
fmt::Display::fmt(&self.context, f) fmt::Display::fmt(&self.context, f)
} }
} }
impl actix_web::error::ResponseError for LemmyError { impl actix_web::error::ResponseError for LemmyError {
fn status_code(&self) -> http::StatusCode { fn status_code(&self) -> http::StatusCode {
if self.error_type == LemmyErrorType::IncorrectLogin { if self.error_type == LemmyErrorType::IncorrectLogin {
return http::StatusCode::UNAUTHORIZED; return http::StatusCode::UNAUTHORIZED;
@ -233,9 +233,9 @@ impl actix_web::error::ResponseError for LemmyError {
fn error_response(&self) -> actix_web::HttpResponse { fn error_response(&self) -> actix_web::HttpResponse {
actix_web::HttpResponse::build(self.status_code()).json(&self.error_type) actix_web::HttpResponse::build(self.status_code()).json(&self.error_type)
} }
} }
impl From<LemmyErrorType> for LemmyError { impl From<LemmyErrorType> for LemmyError {
fn from(error_type: LemmyErrorType) -> Self { fn from(error_type: LemmyErrorType) -> Self {
let inner = anyhow::anyhow!("{}", error_type); let inner = anyhow::anyhow!("{}", error_type);
LemmyError { LemmyError {
@ -244,13 +244,13 @@ impl From<LemmyErrorType> for LemmyError {
context: SpanTrace::capture(), context: SpanTrace::capture(),
} }
} }
} }
pub trait LemmyErrorExt<T, E: Into<anyhow::Error>> { pub trait LemmyErrorExt<T, E: Into<anyhow::Error>> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>; fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>;
} }
impl<T, E: Into<anyhow::Error>> LemmyErrorExt<T, E> for Result<T, E> { impl<T, E: Into<anyhow::Error>> LemmyErrorExt<T, E> for Result<T, E> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> { fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> {
self.map_err(|error| LemmyError { self.map_err(|error| LemmyError {
error_type, error_type,
@ -258,13 +258,13 @@ impl<T, E: Into<anyhow::Error>> LemmyErrorExt<T, E> for Result<T, E> {
context: SpanTrace::capture(), context: SpanTrace::capture(),
}) })
} }
} }
pub trait LemmyErrorExt2<T> { pub trait LemmyErrorExt2<T> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>; fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError>;
fn into_anyhow(self) -> Result<T, anyhow::Error>; fn into_anyhow(self) -> Result<T, anyhow::Error>;
} }
impl<T> LemmyErrorExt2<T> for Result<T, LemmyError> { impl<T> LemmyErrorExt2<T> for Result<T, LemmyError> {
fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> { fn with_lemmy_type(self, error_type: LemmyErrorType) -> Result<T, LemmyError> {
self.map_err(|mut e| { self.map_err(|mut e| {
e.error_type = error_type; e.error_type = error_type;
@ -275,8 +275,7 @@ impl<T> LemmyErrorExt2<T> for Result<T, LemmyError> {
fn into_anyhow(self) -> Result<T, anyhow::Error> { fn into_anyhow(self) -> Result<T, anyhow::Error> {
self.map_err(|e| e.inner) self.map_err(|e| e.inner)
} }
} }
} }
} }