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

162 lines
4.7 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::{
error::{Error as RumaError, ErrorKind},
2022-02-18 15:33:14 +01:00
uiaa::{UiaaInfo, UiaaResponse},
},
2022-10-09 17:25:06 +02:00
OwnedServerName, ServerName,
};
use thiserror::Error;
2022-01-20 11:51:31 +01:00
use tracing::{error, warn};
#[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("{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),
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();
error.message = format!("Answer from {}: {}", origin, error.message);
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);
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 {
Forbidden | GuestAccessForbidden | ThreepidAuthFailed | ThreepidDenied => {
StatusCode::FORBIDDEN
}
2020-09-08 17:32:03 +02:00
Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED,
2020-06-09 15:13:17 +02:00
NotFound => 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),
};
warn!("{}: {}", status_code, message);
2021-06-30 09:52:01 +02:00
RumaResponse(UiaaResponse::MatrixError(RumaError {
2020-06-09 15:13:17 +02:00
kind,
message,
status_code,
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()
}
}