Skip to content

Commit

Permalink
error enum cleanup
Browse files Browse the repository at this point in the history
resolves girlbossceo#483

Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Jun 27, 2024
1 parent 5ad002e commit 86a4100
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 56 deletions.
96 changes: 42 additions & 54 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<T, E = Error> = std::result::Result<T, E>;

#[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),
}
Expand All @@ -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();
}
Expand All @@ -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 {
Expand Down Expand Up @@ -147,6 +133,8 @@ impl From<Error> for RumaResponse<UiaaResponse> {

impl Error {
pub fn to_response(&self) -> RumaResponse<UiaaResponse> {
use ruma::api::client::error::{Error as RumaError, ErrorBody};

if let Self::Uiaa(uiaainfo) = self {
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E = Error> = std::result::Result<T, E>;

#[cfg(not(conduit_mods))]
pub mod mods {
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion src/service/appservice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit 86a4100

Please sign in to comment.