2020-06-09 14:13:17 +01:00
|
|
|
use crate::RumaResponse;
|
|
|
|
use http::StatusCode;
|
|
|
|
use rocket::{
|
|
|
|
response::{self, Responder},
|
|
|
|
Request,
|
|
|
|
};
|
|
|
|
use ruma::api::client::{
|
|
|
|
error::{Error as RumaError, ErrorKind},
|
|
|
|
r0::uiaa::{UiaaInfo, UiaaResponse},
|
|
|
|
};
|
2020-05-03 16:25:31 +01:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
2020-06-09 14:13:17 +01:00
|
|
|
#[error("There was a problem with the connection to the database.")]
|
2020-05-03 16:25:31 +01:00
|
|
|
SledError {
|
|
|
|
#[from]
|
|
|
|
source: sled::Error,
|
|
|
|
},
|
2020-06-09 14:13:17 +01:00
|
|
|
#[error("Could not generate an image.")]
|
2020-05-19 17:31:34 +01:00
|
|
|
ImageError {
|
|
|
|
#[from]
|
|
|
|
source: image::error::ImageError,
|
|
|
|
},
|
2020-06-09 14:13:17 +01:00
|
|
|
#[error("{0}")]
|
|
|
|
BadConfig(&'static str),
|
|
|
|
#[error("{0}")]
|
2020-05-03 16:25:31 +01:00
|
|
|
BadDatabase(&'static str),
|
2020-06-09 14:13:17 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> Responder<'r> for Error {
|
|
|
|
async fn respond_to(self, r: &'r Request<'_>) -> response::Result<'r> {
|
|
|
|
if let Self::Uiaa(uiaainfo) = &self {
|
|
|
|
return RumaResponse::from(UiaaResponse::AuthResponse(uiaainfo.clone()))
|
|
|
|
.respond_to(r)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = format!("{}", self);
|
|
|
|
|
|
|
|
use ErrorKind::*;
|
|
|
|
let (kind, status_code) = match self {
|
|
|
|
Self::BadRequest(kind, _) => (
|
|
|
|
kind,
|
|
|
|
match kind {
|
|
|
|
Forbidden | GuestAccessForbidden | ThreepidAuthFailed | ThreepidDenied => {
|
|
|
|
StatusCode::FORBIDDEN
|
|
|
|
}
|
|
|
|
Unauthorized | UnknownToken | MissingToken => StatusCode::UNAUTHORIZED,
|
|
|
|
NotFound => StatusCode::NOT_FOUND,
|
|
|
|
LimitExceeded => StatusCode::TOO_MANY_REQUESTS,
|
|
|
|
UserDeactivated => StatusCode::FORBIDDEN,
|
|
|
|
TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
|
|
|
|
_ => StatusCode::BAD_REQUEST,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Self::Conflict(_) => (Unknown, StatusCode::CONFLICT),
|
|
|
|
_ => (Unknown, StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
};
|
|
|
|
|
|
|
|
RumaResponse::from(RumaError {
|
|
|
|
kind,
|
|
|
|
message,
|
|
|
|
status_code,
|
|
|
|
})
|
|
|
|
.respond_to(r)
|
|
|
|
.await
|
|
|
|
}
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|