elaborate error macro and apply at various callsites

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-13 21:11:05 +00:00
parent b3f2288d07
commit 05efd9b044
23 changed files with 161 additions and 140 deletions

View file

@ -3,7 +3,7 @@ mod log;
mod panic;
mod response;
use std::{any::Any, convert::Infallible, fmt};
use std::{any::Any, borrow::Cow, convert::Infallible, fmt};
pub use log::*;
@ -64,7 +64,9 @@ pub enum Error {
#[error("{0}")]
Mxid(#[from] ruma::IdParseError),
#[error("{0}: {1}")]
BadRequest(ruma::api::client::error::ErrorKind, &'static str),
BadRequest(ruma::api::client::error::ErrorKind, &'static str), //TODO: remove
#[error("{0}: {1}")]
Request(ruma::api::client::error::ErrorKind, Cow<'static, str>),
#[error("from {0}: {1}")]
Redaction(ruma::OwnedServerName, ruma::canonical_json::RedactionError),
#[error("Remote server {0} responded with: {1}")]
@ -76,11 +78,9 @@ pub enum Error {
#[error("Arithmetic operation failed: {0}")]
Arithmetic(&'static str),
#[error("There was a problem with the '{0}' directive in your configuration: {1}")]
Config(&'static str, String),
Config(&'static str, Cow<'static, str>),
#[error("{0}")]
BadDatabase(&'static str),
#[error("{0}")]
Database(String),
Database(Cow<'static, str>),
#[error("{0}")]
BadServerResponse(&'static str),
#[error("{0}")]
@ -88,14 +88,11 @@ pub enum Error {
// unique / untyped
#[error("{0}")]
Err(String),
Err(Cow<'static, str>),
}
impl Error {
pub fn bad_database(message: &'static str) -> Self {
error!("BadDatabase: {}", message);
Self::BadDatabase(message)
}
pub fn bad_database(message: &'static str) -> Self { crate::err!(Database(error!("{message}"))) }
/// Sanitizes public-facing errors that can leak sensitive information.
pub fn sanitized_string(&self) -> String {
@ -121,7 +118,7 @@ impl Error {
match self {
Self::Federation(_, error) => response::ruma_error_kind(error).clone(),
Self::BadRequest(kind, _) => kind.clone(),
Self::BadRequest(kind, _) | Self::Request(kind, _) => kind.clone(),
_ => Unknown,
}
}
@ -129,7 +126,7 @@ impl Error {
pub fn status_code(&self) -> http::StatusCode {
match self {
Self::Federation(_, ref error) | Self::RumaError(ref error) => error.status_code,
Self::BadRequest(ref kind, _) => response::bad_request_code(kind),
Self::BadRequest(ref kind, _) | Self::Request(ref kind, _) => response::bad_request_code(kind),
Self::Conflict(_) => http::StatusCode::CONFLICT,
_ => http::StatusCode::INTERNAL_SERVER_ERROR,
}