conduit/src/error.rs

139 lines
3.9 KiB
Rust
Raw Permalink Normal View History

use ruma::{
api::client::{
error::{Error as RumaError, ErrorKind},
r0::uiaa::UiaaInfo,
},
ServerName,
};
use thiserror::Error;
use tracing::warn;
#[cfg(feature = "conduit_bin")]
use {
crate::RumaResponse,
http::StatusCode,
rocket::{
response::{self, Responder},
Request,
},
ruma::api::client::r0::uiaa::UiaaResponse,
tracing::error,
};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[cfg(feature = "sled")]
2021-06-08 16:10:00 +00: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,
},
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,
},
#[error("Could not connect to server: {source}")]
ReqwestError {
#[from]
source: reqwest::Error,
},
#[error("{0}")]
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,
},
#[error("{0}")]
BadServerResponse(&'static str),
2020-06-09 13:13:17 +00:00
#[error("{0}")]
BadConfig(&'static str),
#[error("{0}")]
/// Don't create this directly. Use Error::bad_database instead.
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
}
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 07:52:01 +00:00
impl Error {
pub fn to_response(&self) -> RumaResponse<UiaaResponse> {
if let Self::Uiaa(uiaainfo) = self {
2021-06-30 07:52:01 +00:00
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
}
2021-06-30 07:52:01 +00:00
if let Self::FederationError(origin, error) = self {
let mut error = error.clone();
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),
};
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
}
}