1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/utils/error.rs

199 lines
6.1 KiB
Rust
Raw Normal View History

2022-01-20 11:51:31 +01:00
use std::convert::Infallible;
use http::StatusCode;
use ruma::{
api::client::{
2022-12-14 13:09:10 +01:00
error::{Error as RumaError, ErrorBody, ErrorKind},
2022-02-18 15:33:14 +01:00
uiaa::{UiaaInfo, UiaaResponse},
},
2022-10-09 17:26:53 +02:00
OwnedServerName,
};
use thiserror::Error;
use tracing::{error, info};
#[cfg(feature = "persy")]
use persy::PersyError;
2022-01-20 11:51:31 +01:00
use crate::RumaResponse;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Error, Debug)]
pub enum Error {
#[cfg(feature = "sled")]
2021-06-08 18:10:00 +02:00
#[error("There was a problem with the connection to the sled database.")]
SledError {
#[from]
source: sled::Error,
},
2021-07-14 07:07:08 +00:00
#[cfg(feature = "sqlite")]
#[error("There was a problem with the connection to the sqlite database: {source}")]
SqliteError {
#[from]
source: rusqlite::Error,
},
#[cfg(feature = "persy")]
#[error("There was a problem with the connection to the persy database.")]
PersyError { source: PersyError },
2021-07-29 20:17:47 +02:00
#[cfg(feature = "heed")]
#[error("There was a problem with the connection to the heed database: {error}")]
HeedError { error: String },
2021-10-16 15:19:25 +02:00
#[cfg(feature = "rocksdb")]
#[error("There was a problem with the connection to the rocksdb database: {source}")]
RocksDbError {
#[from]
source: rocksdb::Error,
},
2020-06-09 15:13:17 +02:00
#[error("Could not generate an image.")]
2020-05-19 18:31:34 +02:00
ImageError {
#[from]
source: image::error::ImageError,
},
#[error("Could not connect to server: {source}")]
ReqwestError {
#[from]
source: reqwest::Error,
},
#[error("Could build regular expression: {source}")]
RegexError {
#[from]
source: regex::Error,
},
#[error("{0}")]
2022-10-09 17:25:06 +02:00
FederationError(OwnedServerName, RumaError),
2021-06-04 08:06:12 +04:30
#[error("Could not do this io: {source}")]
IoError {
#[from]
source: std::io::Error,
},
#[error("{0}")]
BadServerResponse(&'static str),
2020-06-09 15:13:17 +02:00
#[error("{0}")]
BadConfig(&'static str),
#[error("{0}")]
/// Don't create this directly. Use Error::bad_database instead.
BadDatabase(&'static str),
2020-06-09 15:13:17 +02:00
#[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
2022-01-20 11:51:31 +01:00
#[cfg(feature = "conduit_bin")]
#[error("{0}")]
ExtensionError(#[from] axum::extract::rejection::ExtensionRejection),
2022-02-12 02:06:30 +01:00
#[cfg(feature = "conduit_bin")]
#[error("{0}")]
PathError(#[from] axum::extract::rejection::PathRejection),
#[error("{0}")]
AdminCommand(&'static str),
#[error("from {0}: {1}")]
RedactionError(OwnedServerName, ruma::canonical_json::RedactionError),
#[error("{0} in {1}")]
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
2020-06-09 15:13:17 +02:00
}
impl Error {
pub fn bad_database(message: &'static str) -> Self {
error!("BadDatabase: {}", message);
Self::BadDatabase(message)
}
pub fn bad_config(message: &'static str) -> Self {
error!("BadConfig: {}", message);
Self::BadConfig(message)
}
}
2021-06-30 09:52:01 +02:00
impl Error {
pub fn to_response(&self) -> RumaResponse<UiaaResponse> {
if let Self::Uiaa(uiaainfo) = self {
2021-06-30 09:52:01 +02:00
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
}
2021-06-30 09:52:01 +02:00
if let Self::FederationError(origin, error) = self {
let mut error = error.clone();
2022-12-17 09:21:19 +01:00
error.body = ErrorBody::Standard {
kind: Unknown,
message: format!("Answer from {origin}: {error}"),
2022-12-17 09:21:19 +01:00
};
2021-06-30 09:52:01 +02:00
return RumaResponse(UiaaResponse::MatrixError(error));
2020-06-09 15:13:17 +02:00
}
let message = format!("{self}");
2020-06-09 15:13:17 +02:00
use ErrorKind::*;
let (kind, status_code) = match self {
Self::BadRequest(kind, _) => (
2020-09-08 17:32:03 +02:00
kind.clone(),
2020-06-09 15:13:17 +02:00
match kind {
WrongRoomKeysVersion { .. }
| Forbidden
| GuestAccessForbidden
| ThreepidAuthFailed
| ThreepidDenied => StatusCode::FORBIDDEN,
2020-09-08 17:32:03 +02:00
Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED,
NotFound | Unrecognized => StatusCode::NOT_FOUND,
2020-09-08 17:32:03 +02:00
LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS,
2020-06-09 15:13:17 +02:00
UserDeactivated => StatusCode::FORBIDDEN,
TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
_ => StatusCode::BAD_REQUEST,
},
),
Self::Conflict(_) => (Unknown, StatusCode::CONFLICT),
_ => (Unknown, StatusCode::INTERNAL_SERVER_ERROR),
};
info!("Returning an error: {}: {}", status_code, message);
2021-06-30 09:52:01 +02:00
RumaResponse(UiaaResponse::MatrixError(RumaError {
2022-12-14 13:09:10 +01:00
body: ErrorBody::Standard { kind, message },
2020-06-09 15:13:17 +02:00
status_code,
2021-06-30 09:52:01 +02:00
}))
}
/// 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 = "sled")]
Self::SledError { .. } => db_error,
#[cfg(feature = "sqlite")]
Self::SqliteError { .. } => db_error,
#[cfg(feature = "persy")]
Self::PersyError { .. } => db_error,
#[cfg(feature = "heed")]
Self::HeedError => db_error,
#[cfg(feature = "rocksdb")]
Self::RocksDbError { .. } => db_error,
Self::IoError { .. } => db_error,
Self::BadConfig { .. } => db_error,
Self::BadDatabase { .. } => db_error,
_ => self.to_string(),
}
}
2021-06-30 09:52:01 +02:00
}
#[cfg(feature = "persy")]
impl<T: Into<PersyError>> From<persy::PE<T>> for Error {
fn from(err: persy::PE<T>) -> Self {
Error::PersyError {
source: err.error().into(),
}
}
}
2022-01-20 11:51:31 +01:00
impl From<Infallible> for Error {
fn from(i: Infallible) -> Self {
match i {}
}
}
#[cfg(feature = "conduit_bin")]
impl axum::response::IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
self.to_response().into_response()
}
}