error enum cleanup

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-14 18:29:21 +00:00
parent fbcdb3860a
commit a6edaad6fc
4 changed files with 54 additions and 67 deletions

View file

@ -63,13 +63,6 @@ regex.workspace = true
reqwest.workspace = true reqwest.workspace = true
ring.workspace = true ring.workspace = true
ruma.workspace = true ruma.workspace = true
<<<<<<< HEAD
rust-rocksdb.optional = true
rust-rocksdb.workspace = true
=======
rusqlite.optional = true
rusqlite.workspace = true
>>>>>>> 6963b38f (convert rocksdb errors locally; remove from Error.)
sanitize-filename.workspace = true sanitize-filename.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde_regex.workspace = true serde_regex.workspace = true

View file

@ -7,7 +7,11 @@ use http_body_util::Full;
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
error::{Error as RumaError, ErrorBody, ErrorKind}, error::ErrorKind::{
Forbidden, GuestAccessForbidden, LimitExceeded, MissingToken, NotFound, ThreepidAuthFailed,
ThreepidDenied, TooLarge, Unauthorized, Unknown, UnknownToken, Unrecognized, UserDeactivated,
WrongRoomKeysVersion,
},
uiaa::{UiaaInfo, UiaaResponse}, uiaa::{UiaaInfo, UiaaResponse},
}, },
OutgoingResponse, OutgoingResponse,
@ -16,68 +20,56 @@ use ruma::{
}; };
use thiserror::Error; use thiserror::Error;
use tracing::error; use tracing::error;
use ErrorKind::{
Forbidden, GuestAccessForbidden, LimitExceeded, MissingToken, NotFound, ThreepidAuthFailed, ThreepidDenied,
TooLarge, Unauthorized, Unknown, UnknownToken, Unrecognized, UserDeactivated, WrongRoomKeysVersion,
};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Error)] #[derive(Error)]
pub enum Error { pub enum Error {
// std
#[error("{0}")] #[error("{0}")]
Database(String), Fmt(#[from] fmt::Error),
#[error("Could not generate an image: {source}")] #[error("I/O error: {0}")]
Image { Io(#[from] std::io::Error),
#[from]
source: image::error::ImageError, // third-party
}, #[error("Regex error: {0}")]
#[error("Could not connect to server: {source}")] Regex(#[from] regex::Error),
Reqwest { #[error("Tracing filter error: {0}")]
#[from] TracingFilter(#[from] tracing_subscriber::filter::ParseError),
source: reqwest::Error, #[error("Image error: {0}")]
}, Image(#[from] image::error::ImageError),
#[error("Could build regular expression: {source}")] #[error("Request error: {0}")]
Regex { Reqwest(#[from] reqwest::Error),
#[from]
source: regex::Error,
},
#[error("Remote server {0} responded with: {1}")]
Federation(OwnedServerName, RumaError),
#[error("Could not do this io: {source}")]
Io {
#[from]
source: std::io::Error,
},
#[error("There was a problem with your configuration: {0}")]
BadConfig(String),
#[error("{0}")]
BadServerResponse(&'static str),
#[error("{0}")]
/// Don't create this directly. Use Error::bad_database instead.
BadDatabase(&'static str),
#[error("uiaa")]
Uiaa(UiaaInfo),
#[error("{0}: {1}")]
BadRequest(ErrorKind, &'static str),
#[error("{0}")]
Conflict(&'static str), // This is only needed for when a room alias already exists
#[error("{0}")] #[error("{0}")]
Extension(#[from] axum::extract::rejection::ExtensionRejection), Extension(#[from] axum::extract::rejection::ExtensionRejection),
#[error("{0}")] #[error("{0}")]
Path(#[from] axum::extract::rejection::PathRejection), Path(#[from] axum::extract::rejection::PathRejection),
#[error("from {0}: {1}")]
Redaction(OwnedServerName, ruma::canonical_json::RedactionError), // ruma
#[error("{0} in {1}")]
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
#[error("{0}")]
TracingFilter(#[from] tracing_subscriber::filter::ParseError),
#[error("{0}")]
AdminCommand(&'static str),
#[error("{0}")]
Fmt(#[from] fmt::Error),
#[error("{0}")] #[error("{0}")]
Mxid(#[from] ruma::IdParseError), Mxid(#[from] ruma::IdParseError),
#[error("{0}: {1}")]
BadRequest(ruma::api::client::error::ErrorKind, &'static str),
#[error("from {0}: {1}")]
Redaction(OwnedServerName, ruma::canonical_json::RedactionError),
#[error("Remote server {0} responded with: {1}")]
Federation(OwnedServerName, ruma::api::client::error::Error),
#[error("{0} in {1}")]
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
// conduwuit
#[error("There was a problem with your configuration: {0}")]
BadConfig(String),
#[error("{0}")]
BadDatabase(&'static str),
#[error("{0}")]
Database(String),
#[error("{0}")]
BadServerResponse(&'static str),
#[error("{0}")]
Conflict(&'static str), // This is only needed for when a room alias already exists
#[error("uiaa")]
Uiaa(UiaaInfo),
// unique / untyped
#[error("{0}")] #[error("{0}")]
Err(String), Err(String),
} }
@ -94,7 +86,7 @@ impl Error {
} }
/// Returns the Matrix error code / error kind /// Returns the Matrix error code / error kind
pub fn error_code(&self) -> ErrorKind { pub fn error_code(&self) -> ruma::api::client::error::ErrorKind {
if let Self::Federation(_, error) = self { if let Self::Federation(_, error) = self {
return error.error_kind().unwrap_or_else(|| &Unknown).clone(); return error.error_kind().unwrap_or_else(|| &Unknown).clone();
} }
@ -107,15 +99,13 @@ impl Error {
/// Sanitizes public-facing errors that can leak sensitive information. /// Sanitizes public-facing errors that can leak sensitive information.
pub fn sanitized_error(&self) -> String { pub fn sanitized_error(&self) -> String {
let db_error = String::from("Database or I/O error occurred.");
match self { match self {
Self::Database { Self::Database {
.. ..
} => db_error, } => String::from("Database error occurred."),
Self::Io { Self::Io {
.. ..
} => db_error, } => String::from("I/O error occurred."),
_ => self.to_string(), _ => self.to_string(),
} }
} }
@ -142,6 +132,8 @@ impl From<Error> for RumaResponse<UiaaResponse> {
impl Error { impl Error {
pub fn to_response(&self) -> RumaResponse<UiaaResponse> { pub fn to_response(&self) -> RumaResponse<UiaaResponse> {
use ruma::api::client::error::{Error as RumaError, ErrorBody};
if let Self::Uiaa(uiaainfo) = self { if let Self::Uiaa(uiaainfo) = self {
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone())); return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
} }

View file

@ -10,10 +10,12 @@ pub mod utils;
pub mod version; pub mod version;
pub use config::Config; pub use config::Config;
pub use error::{Error, Result, RumaResponse}; pub use error::{Error, RumaResponse};
pub use pducount::PduCount; pub use pducount::PduCount;
pub use server::Server; pub use server::Server;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[cfg(not(conduit_mods))] #[cfg(not(conduit_mods))]
pub mod mods { pub mod mods {
#[macro_export] #[macro_export]

View file

@ -159,7 +159,7 @@ impl Service {
.write() .write()
.await .await
.remove(service_name) .remove(service_name)
.ok_or_else(|| crate::Error::AdminCommand("Appservice not found"))?; .ok_or_else(|| crate::Error::Err("Appservice not found".to_owned()))?;
// remove the appservice from the database // remove the appservice from the database
self.db.unregister_appservice(service_name)?; self.db.unregister_appservice(service_name)?;