diff --git a/src/core/error.rs b/src/core/error.rs index 4fafd1e32..c3e9c1d00 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -7,7 +7,11 @@ use http_body_util::Full; use ruma::{ api::{ 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}, }, OutgoingResponse, @@ -16,72 +20,56 @@ use ruma::{ }; use thiserror::Error; use tracing::error; -use ErrorKind::{ - Forbidden, GuestAccessForbidden, LimitExceeded, MissingToken, NotFound, ThreepidAuthFailed, ThreepidDenied, - TooLarge, Unauthorized, Unknown, UnknownToken, Unrecognized, UserDeactivated, WrongRoomKeysVersion, -}; - -pub type Result = std::result::Result; #[derive(Error)] pub enum Error { - #[cfg(feature = "rocksdb")] - #[error("There was a problem with the connection to the rocksdb database: {source}")] - RocksDb { - #[from] - source: rust_rocksdb::Error, - }, - #[error("Could not generate an image: {source}")] - Image { - #[from] - source: image::error::ImageError, - }, - #[error("Could not connect to server: {source}")] - Reqwest { - #[from] - source: reqwest::Error, - }, - #[error("Could build regular expression: {source}")] - Regex { - #[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), + // std #[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 + Fmt(#[from] fmt::Error), + #[error("I/O error: {0}")] + Io(#[from] std::io::Error), + + // third-party + #[error("Regex error: {0}")] + Regex(#[from] regex::Error), + #[error("Tracing filter error: {0}")] + TracingFilter(#[from] tracing_subscriber::filter::ParseError), + #[error("Image error: {0}")] + Image(#[from] image::error::ImageError), + #[error("Request error: {0}")] + Reqwest(#[from] reqwest::Error), #[error("{0}")] Extension(#[from] axum::extract::rejection::ExtensionRejection), #[error("{0}")] Path(#[from] axum::extract::rejection::PathRejection), + + // ruma + #[error("{0}")] + 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}")] - TracingFilter(#[from] tracing_subscriber::filter::ParseError), + BadDatabase(&'static str), #[error("{0}")] - AdminCommand(&'static str), + Database(String), #[error("{0}")] - Fmt(#[from] fmt::Error), + BadServerResponse(&'static str), #[error("{0}")] - Mxid(#[from] ruma::IdParseError), + Conflict(&'static str), // This is only needed for when a room alias already exists + #[error("uiaa")] + Uiaa(UiaaInfo), + + // unique / untyped #[error("{0}")] Err(String), } @@ -98,7 +86,7 @@ impl Error { } /// 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 { return error.error_kind().unwrap_or_else(|| &Unknown).clone(); } @@ -111,8 +99,6 @@ impl Error { /// Sanitizes public-facing errors that can leak sensitive information. pub fn sanitized_error(&self) -> String { - let db_error = String::from("Database or I/O error occurred."); - match self { #[cfg(feature = "rocksdb")] Self::RocksDb { @@ -147,6 +133,8 @@ impl From for RumaResponse { impl Error { pub fn to_response(&self) -> RumaResponse { + use ruma::api::client::error::{Error as RumaError, ErrorBody}; + if let Self::Uiaa(uiaainfo) = self { return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone())); } diff --git a/src/core/mod.rs b/src/core/mod.rs index 52cf2d8a2..5ffe4cb9f 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -10,11 +10,13 @@ pub mod utils; pub mod version; pub use config::Config; -pub use error::{Error, Result, RumaResponse}; +pub use error::{Error, RumaResponse}; pub use pducount::PduCount; pub use server::Server; pub use version::version; +pub type Result = std::result::Result; + #[cfg(not(conduit_mods))] pub mod mods { #[macro_export] diff --git a/src/service/appservice/mod.rs b/src/service/appservice/mod.rs index 1f29c4154..941333960 100644 --- a/src/service/appservice/mod.rs +++ b/src/service/appservice/mod.rs @@ -159,7 +159,7 @@ impl Service { .write() .await .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 self.db.unregister_appservice(service_name)?;