add RumaError to Error; encapsulate RumaResponse in api

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-08 18:54:42 +00:00
parent 7ba0777bd3
commit a43c78e55f
5 changed files with 45 additions and 35 deletions

View file

@ -9,8 +9,7 @@ extern crate conduit_service as service;
pub(crate) use conduit::{debug_info, debug_warn, utils, Error, Result};
pub(crate) use service::{pdu::PduEvent, services, user_is_local};
pub(crate) use self::router::Ruma;
pub use self::router::RumaResponse;
pub(crate) use self::router::{Ruma, RumaResponse};
conduit::mod_ctor! {}
conduit::mod_dtor! {}

View file

@ -13,9 +13,8 @@ use ruma::{
CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, UserId,
};
pub(super) use self::handler::RouterExt;
pub use self::response::RumaResponse;
use self::{auth::Auth, request::Request};
pub(super) use self::{handler::RouterExt, response::RumaResponse};
use crate::{service::appservice::RegistrationInfo, services, Error, Result};
/// Extractor for Ruma request structs

View file

@ -5,8 +5,7 @@ use http::StatusCode;
use http_body_util::Full;
use ruma::api::{client::uiaa::UiaaResponse, OutgoingResponse};
#[derive(Clone)]
pub struct RumaResponse<T>(pub T);
pub(crate) struct RumaResponse<T>(pub(crate) T);
impl From<Error> for RumaResponse<UiaaResponse> {
fn from(t: Error) -> Self { Self(t.into()) }

View file

@ -4,17 +4,13 @@ use bytes::BytesMut;
use http::StatusCode;
use http_body_util::Full;
use ruma::{
api::{
client::uiaa::{UiaaInfo, UiaaResponse},
OutgoingResponse,
},
api::{client::uiaa::UiaaResponse, OutgoingResponse},
OwnedServerName,
};
use thiserror::Error;
use crate::{debug_error, error};
#[derive(Error)]
#[derive(thiserror::Error)]
pub enum Error {
// std
#[error("{0}")]
@ -47,10 +43,16 @@ pub enum Error {
Extension(#[from] axum::extract::rejection::ExtensionRejection),
#[error("{0}")]
Path(#[from] axum::extract::rejection::PathRejection),
#[error("{0}")]
Http(#[from] http::Error),
// ruma
#[error("{0}")]
IntoHttpError(#[from] ruma::api::error::IntoHttpError),
#[error("{0}")]
RumaError(#[from] ruma::api::client::error::Error),
#[error("uiaa")]
Uiaa(UiaaInfo),
Uiaa(ruma::api::client::uiaa::UiaaInfo),
#[error("{0}")]
Mxid(#[from] ruma::IdParseError),
#[error("{0}: {1}")]
@ -98,7 +100,7 @@ impl Error {
use ruma::api::client::error::ErrorKind::Unknown;
match self {
Self::Federation(_, err) => err.error_kind().unwrap_or(&Unknown).clone(),
Self::Federation(_, error) => ruma_error_kind(error).clone(),
Self::BadRequest(kind, _) => kind.clone(),
_ => Unknown,
}
@ -134,37 +136,35 @@ impl axum::response::IntoResponse for Error {
impl From<Error> for UiaaResponse {
fn from(error: Error) -> Self {
use ruma::api::client::error::{Error as RumaError, ErrorBody, ErrorKind::Unknown};
if let Error::Uiaa(uiaainfo) = error {
return Self::AuthResponse(uiaainfo);
}
let kind = match &error {
Error::Federation(_, ref error) => error.error_kind().unwrap_or(&Unknown),
Error::Federation(_, ref error) | Error::RumaError(ref error) => ruma_error_kind(error),
Error::BadRequest(kind, _) => kind,
_ => &Unknown,
_ => &ruma::api::client::error::ErrorKind::Unknown,
};
let status_code = match &error {
Error::Federation(_, ref error) => error.status_code,
Error::Federation(_, ref error) | Error::RumaError(ref error) => error.status_code,
Error::BadRequest(ref kind, _) => bad_request_code(kind),
Error::Conflict(_) => StatusCode::CONFLICT,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
let message = if let Error::Federation(ref origin, ref error) = &error {
format!("Answer from {origin}: {error}")
} else {
format!("{error}")
let message = match &error {
Error::Federation(ref origin, ref error) => format!("Answer from {origin}: {error}"),
Error::RumaError(ref error) => ruma_error_message(error),
_ => format!("{error}"),
};
let body = ErrorBody::Standard {
let body = ruma::api::client::error::ErrorBody::Standard {
kind: kind.clone(),
message,
};
Self::MatrixError(RumaError {
Self::MatrixError(ruma::api::client::error::Error {
status_code,
body,
})
@ -204,6 +204,23 @@ fn bad_request_code(kind: &ruma::api::client::error::ErrorKind) -> StatusCode {
}
}
fn ruma_error_message(error: &ruma::api::client::error::Error) -> String {
if let ruma::api::client::error::ErrorBody::Standard {
message,
..
} = &error.body
{
return message.to_string();
}
format!("{error}")
}
fn ruma_error_kind(e: &ruma::api::client::error::Error) -> &ruma::api::client::error::ErrorKind {
e.error_kind()
.unwrap_or(&ruma::api::client::error::ErrorKind::Unknown)
}
#[inline]
pub fn log(e: Error) {
error!("{e}");

View file

@ -1,13 +1,9 @@
use std::sync::{atomic::Ordering, Arc};
use axum::{extract::State, response::IntoResponse};
use conduit::{debug, debug_error, debug_warn, defer, error, trace, Result, Server};
use conduit_api::RumaResponse;
use conduit::{debug, debug_error, debug_warn, defer, error, trace, Error, Result, Server};
use http::{Method, StatusCode, Uri};
use ruma::api::client::{
error::{Error as RumaError, ErrorBody, ErrorKind},
uiaa::UiaaResponse,
};
use ruma::api::client::error::{Error as RumaError, ErrorBody, ErrorKind};
#[tracing::instrument(skip_all, level = "debug")]
pub(crate) async fn spawn(
@ -66,15 +62,15 @@ fn handle_result(
) -> Result<axum::response::Response, StatusCode> {
handle_result_log(method, uri, &result);
match result.status() {
StatusCode::METHOD_NOT_ALLOWED => handle_result_403(method, uri, &result),
StatusCode::METHOD_NOT_ALLOWED => handle_result_405(method, uri, &result),
_ => Ok(result),
}
}
fn handle_result_403(
fn handle_result_405(
_method: &Method, _uri: &Uri, result: &axum::response::Response,
) -> Result<axum::response::Response, StatusCode> {
let error = UiaaResponse::MatrixError(RumaError {
let error = Error::RumaError(RumaError {
status_code: result.status(),
body: ErrorBody::Standard {
kind: ErrorKind::Unrecognized,
@ -82,7 +78,7 @@ fn handle_result_403(
},
});
Ok(RumaResponse(error).into_response())
Ok(error.into_response())
}
fn handle_result_log(method: &Method, uri: &Uri, result: &axum::response::Response) {