2021-05-23 14:45:32 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::{Error as RumaError, ErrorKind},
|
|
|
|
r0::uiaa::UiaaInfo,
|
|
|
|
},
|
|
|
|
ServerName,
|
|
|
|
};
|
2020-05-03 15:25:31 +00:00
|
|
|
use thiserror::Error;
|
2021-07-29 06:36:01 +00:00
|
|
|
use tracing::warn;
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-07-26 03:08:00 +00:00
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use {
|
|
|
|
crate::RumaResponse,
|
|
|
|
http::StatusCode,
|
|
|
|
rocket::{
|
|
|
|
response::{self, Responder},
|
|
|
|
Request,
|
|
|
|
},
|
2021-05-23 14:45:32 +00:00
|
|
|
ruma::api::client::r0::uiaa::UiaaResponse,
|
2021-07-29 06:36:01 +00:00
|
|
|
tracing::error,
|
2020-07-26 03:08:00 +00:00
|
|
|
};
|
|
|
|
|
2020-05-03 15:25:31 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
2021-06-12 13:04:28 +00:00
|
|
|
#[cfg(feature = "sled")]
|
2021-06-08 16:10:00 +00:00
|
|
|
#[error("There was a problem with the connection to the sled database.")]
|
2020-05-03 15:25:31 +00:00
|
|
|
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,
|
|
|
|
},
|
2021-07-29 18:17:47 +00:00
|
|
|
#[cfg(feature = "heed")]
|
|
|
|
#[error("There was a problem with the connection to the heed database: {error}")]
|
|
|
|
HeedError { error: String },
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("Could not generate an image.")]
|
2020-05-19 16:31:34 +00:00
|
|
|
ImageError {
|
|
|
|
#[from]
|
|
|
|
source: image::error::ImageError,
|
|
|
|
},
|
2020-12-23 14:53:41 +00:00
|
|
|
#[error("Could not connect to server: {source}")]
|
2020-08-14 09:31:31 +00:00
|
|
|
ReqwestError {
|
|
|
|
#[from]
|
|
|
|
source: reqwest::Error,
|
|
|
|
},
|
|
|
|
#[error("{0}")]
|
2021-05-23 14:45:32 +00:00
|
|
|
FederationError(Box<ServerName>, RumaError),
|
2021-06-04 03:36:12 +00:00
|
|
|
#[error("Could not do this io: {source}")]
|
|
|
|
IoError {
|
|
|
|
#[from]
|
|
|
|
source: std::io::Error,
|
|
|
|
},
|
2021-05-23 14:45:32 +00:00
|
|
|
#[error("{0}")]
|
2020-08-14 09:31:31 +00:00
|
|
|
BadServerResponse(&'static str),
|
2020-06-09 13:13:17 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
BadConfig(&'static str),
|
|
|
|
#[error("{0}")]
|
2020-06-11 08:03:08 +00:00
|
|
|
/// Don't create this directly. Use Error::bad_database instead.
|
2020-05-03 15:25:31 +00:00
|
|
|
BadDatabase(&'static str),
|
2020-06-09 13:13:17 +00: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
|
|
|
|
}
|
|
|
|
|
2020-06-11 08:03:08 +00:00
|
|
|
impl Error {
|
|
|
|
pub fn bad_database(message: &'static str) -> Self {
|
|
|
|
error!("BadDatabase: {}", message);
|
|
|
|
Self::BadDatabase(message)
|
|
|
|
}
|
2020-11-14 22:13:06 +00:00
|
|
|
|
|
|
|
pub fn bad_config(message: &'static str) -> Self {
|
|
|
|
error!("BadConfig: {}", message);
|
|
|
|
Self::BadConfig(message)
|
|
|
|
}
|
2020-06-11 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 07:52:01 +00:00
|
|
|
impl Error {
|
|
|
|
pub fn to_response(&self) -> RumaResponse<UiaaResponse> {
|
2021-05-23 14:45:32 +00:00
|
|
|
if let Self::Uiaa(uiaainfo) = self {
|
2021-06-30 07:52:01 +00:00
|
|
|
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
|
2021-05-23 14:45:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 07:52:01 +00:00
|
|
|
if let Self::FederationError(origin, error) = self {
|
|
|
|
let mut error = error.clone();
|
2021-05-23 14:45:32 +00:00
|
|
|
error.message = format!("Answer from {}: {}", origin, error.message);
|
2021-06-30 07:52:01 +00:00
|
|
|
return RumaResponse(UiaaResponse::MatrixError(error));
|
2020-06-09 13:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let message = format!("{}", self);
|
|
|
|
|
|
|
|
use ErrorKind::*;
|
|
|
|
let (kind, status_code) = match self {
|
|
|
|
Self::BadRequest(kind, _) => (
|
2020-09-08 15:32:03 +00:00
|
|
|
kind.clone(),
|
2020-06-09 13:13:17 +00:00
|
|
|
match kind {
|
|
|
|
Forbidden | GuestAccessForbidden | ThreepidAuthFailed | ThreepidDenied => {
|
|
|
|
StatusCode::FORBIDDEN
|
|
|
|
}
|
2020-09-08 15:32:03 +00:00
|
|
|
Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED,
|
2020-06-09 13:13:17 +00:00
|
|
|
NotFound => StatusCode::NOT_FOUND,
|
2020-09-08 15:32:03 +00:00
|
|
|
LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS,
|
2020-06-09 13:13:17 +00:00
|
|
|
UserDeactivated => StatusCode::FORBIDDEN,
|
|
|
|
TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
|
|
|
|
_ => StatusCode::BAD_REQUEST,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Self::Conflict(_) => (Unknown, StatusCode::CONFLICT),
|
|
|
|
_ => (Unknown, StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
};
|
|
|
|
|
2021-05-12 18:04:28 +00:00
|
|
|
warn!("{}: {}", status_code, message);
|
|
|
|
|
2021-06-30 07:52:01 +00:00
|
|
|
RumaResponse(UiaaResponse::MatrixError(RumaError {
|
2020-06-09 13:13:17 +00:00
|
|
|
kind,
|
|
|
|
message,
|
|
|
|
status_code,
|
2021-06-30 07:52:01 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
impl<'r, 'o> Responder<'r, 'o> for Error
|
|
|
|
where
|
|
|
|
'o: 'r,
|
|
|
|
{
|
|
|
|
fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> {
|
|
|
|
self.to_response().respond_to(r)
|
2020-06-09 13:13:17 +00:00
|
|
|
}
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|