add RumaError to Error; encapsulate RumaResponse in api
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
7ba0777bd3
commit
a43c78e55f
5 changed files with 45 additions and 35 deletions
|
@ -9,8 +9,7 @@ extern crate conduit_service as service;
|
||||||
pub(crate) use conduit::{debug_info, debug_warn, utils, Error, Result};
|
pub(crate) use conduit::{debug_info, debug_warn, utils, Error, Result};
|
||||||
pub(crate) use service::{pdu::PduEvent, services, user_is_local};
|
pub(crate) use service::{pdu::PduEvent, services, user_is_local};
|
||||||
|
|
||||||
pub(crate) use self::router::Ruma;
|
pub(crate) use self::router::{Ruma, RumaResponse};
|
||||||
pub use self::router::RumaResponse;
|
|
||||||
|
|
||||||
conduit::mod_ctor! {}
|
conduit::mod_ctor! {}
|
||||||
conduit::mod_dtor! {}
|
conduit::mod_dtor! {}
|
||||||
|
|
|
@ -13,9 +13,8 @@ use ruma::{
|
||||||
CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, UserId,
|
CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, UserId,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(super) use self::handler::RouterExt;
|
|
||||||
pub use self::response::RumaResponse;
|
|
||||||
use self::{auth::Auth, request::Request};
|
use self::{auth::Auth, request::Request};
|
||||||
|
pub(super) use self::{handler::RouterExt, response::RumaResponse};
|
||||||
use crate::{service::appservice::RegistrationInfo, services, Error, Result};
|
use crate::{service::appservice::RegistrationInfo, services, Error, Result};
|
||||||
|
|
||||||
/// Extractor for Ruma request structs
|
/// Extractor for Ruma request structs
|
||||||
|
|
|
@ -5,8 +5,7 @@ use http::StatusCode;
|
||||||
use http_body_util::Full;
|
use http_body_util::Full;
|
||||||
use ruma::api::{client::uiaa::UiaaResponse, OutgoingResponse};
|
use ruma::api::{client::uiaa::UiaaResponse, OutgoingResponse};
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub(crate) struct RumaResponse<T>(pub(crate) T);
|
||||||
pub struct RumaResponse<T>(pub T);
|
|
||||||
|
|
||||||
impl From<Error> for RumaResponse<UiaaResponse> {
|
impl From<Error> for RumaResponse<UiaaResponse> {
|
||||||
fn from(t: Error) -> Self { Self(t.into()) }
|
fn from(t: Error) -> Self { Self(t.into()) }
|
||||||
|
|
|
@ -4,17 +4,13 @@ use bytes::BytesMut;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use http_body_util::Full;
|
use http_body_util::Full;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::{
|
api::{client::uiaa::UiaaResponse, OutgoingResponse},
|
||||||
client::uiaa::{UiaaInfo, UiaaResponse},
|
|
||||||
OutgoingResponse,
|
|
||||||
},
|
|
||||||
OwnedServerName,
|
OwnedServerName,
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use crate::{debug_error, error};
|
use crate::{debug_error, error};
|
||||||
|
|
||||||
#[derive(Error)]
|
#[derive(thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
// std
|
// std
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
|
@ -47,10 +43,16 @@ pub enum Error {
|
||||||
Extension(#[from] axum::extract::rejection::ExtensionRejection),
|
Extension(#[from] axum::extract::rejection::ExtensionRejection),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Path(#[from] axum::extract::rejection::PathRejection),
|
Path(#[from] axum::extract::rejection::PathRejection),
|
||||||
|
#[error("{0}")]
|
||||||
|
Http(#[from] http::Error),
|
||||||
|
|
||||||
// ruma
|
// ruma
|
||||||
|
#[error("{0}")]
|
||||||
|
IntoHttpError(#[from] ruma::api::error::IntoHttpError),
|
||||||
|
#[error("{0}")]
|
||||||
|
RumaError(#[from] ruma::api::client::error::Error),
|
||||||
#[error("uiaa")]
|
#[error("uiaa")]
|
||||||
Uiaa(UiaaInfo),
|
Uiaa(ruma::api::client::uiaa::UiaaInfo),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Mxid(#[from] ruma::IdParseError),
|
Mxid(#[from] ruma::IdParseError),
|
||||||
#[error("{0}: {1}")]
|
#[error("{0}: {1}")]
|
||||||
|
@ -98,7 +100,7 @@ impl Error {
|
||||||
use ruma::api::client::error::ErrorKind::Unknown;
|
use ruma::api::client::error::ErrorKind::Unknown;
|
||||||
|
|
||||||
match self {
|
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(),
|
Self::BadRequest(kind, _) => kind.clone(),
|
||||||
_ => Unknown,
|
_ => Unknown,
|
||||||
}
|
}
|
||||||
|
@ -134,37 +136,35 @@ impl axum::response::IntoResponse for Error {
|
||||||
|
|
||||||
impl From<Error> for UiaaResponse {
|
impl From<Error> for UiaaResponse {
|
||||||
fn from(error: Error) -> Self {
|
fn from(error: Error) -> Self {
|
||||||
use ruma::api::client::error::{Error as RumaError, ErrorBody, ErrorKind::Unknown};
|
|
||||||
|
|
||||||
if let Error::Uiaa(uiaainfo) = error {
|
if let Error::Uiaa(uiaainfo) = error {
|
||||||
return Self::AuthResponse(uiaainfo);
|
return Self::AuthResponse(uiaainfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
let kind = match &error {
|
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,
|
Error::BadRequest(kind, _) => kind,
|
||||||
_ => &Unknown,
|
_ => &ruma::api::client::error::ErrorKind::Unknown,
|
||||||
};
|
};
|
||||||
|
|
||||||
let status_code = match &error {
|
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::BadRequest(ref kind, _) => bad_request_code(kind),
|
||||||
Error::Conflict(_) => StatusCode::CONFLICT,
|
Error::Conflict(_) => StatusCode::CONFLICT,
|
||||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
};
|
};
|
||||||
|
|
||||||
let message = if let Error::Federation(ref origin, ref error) = &error {
|
let message = match &error {
|
||||||
format!("Answer from {origin}: {error}")
|
Error::Federation(ref origin, ref error) => format!("Answer from {origin}: {error}"),
|
||||||
} else {
|
Error::RumaError(ref error) => ruma_error_message(error),
|
||||||
format!("{error}")
|
_ => format!("{error}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = ErrorBody::Standard {
|
let body = ruma::api::client::error::ErrorBody::Standard {
|
||||||
kind: kind.clone(),
|
kind: kind.clone(),
|
||||||
message,
|
message,
|
||||||
};
|
};
|
||||||
|
|
||||||
Self::MatrixError(RumaError {
|
Self::MatrixError(ruma::api::client::error::Error {
|
||||||
status_code,
|
status_code,
|
||||||
body,
|
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]
|
#[inline]
|
||||||
pub fn log(e: Error) {
|
pub fn log(e: Error) {
|
||||||
error!("{e}");
|
error!("{e}");
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
use std::sync::{atomic::Ordering, Arc};
|
use std::sync::{atomic::Ordering, Arc};
|
||||||
|
|
||||||
use axum::{extract::State, response::IntoResponse};
|
use axum::{extract::State, response::IntoResponse};
|
||||||
use conduit::{debug, debug_error, debug_warn, defer, error, trace, Result, Server};
|
use conduit::{debug, debug_error, debug_warn, defer, error, trace, Error, Result, Server};
|
||||||
use conduit_api::RumaResponse;
|
|
||||||
use http::{Method, StatusCode, Uri};
|
use http::{Method, StatusCode, Uri};
|
||||||
use ruma::api::client::{
|
use ruma::api::client::error::{Error as RumaError, ErrorBody, ErrorKind};
|
||||||
error::{Error as RumaError, ErrorBody, ErrorKind},
|
|
||||||
uiaa::UiaaResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, level = "debug")]
|
#[tracing::instrument(skip_all, level = "debug")]
|
||||||
pub(crate) async fn spawn(
|
pub(crate) async fn spawn(
|
||||||
|
@ -66,15 +62,15 @@ fn handle_result(
|
||||||
) -> Result<axum::response::Response, StatusCode> {
|
) -> Result<axum::response::Response, StatusCode> {
|
||||||
handle_result_log(method, uri, &result);
|
handle_result_log(method, uri, &result);
|
||||||
match result.status() {
|
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),
|
_ => Ok(result),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_result_403(
|
fn handle_result_405(
|
||||||
_method: &Method, _uri: &Uri, result: &axum::response::Response,
|
_method: &Method, _uri: &Uri, result: &axum::response::Response,
|
||||||
) -> Result<axum::response::Response, StatusCode> {
|
) -> Result<axum::response::Response, StatusCode> {
|
||||||
let error = UiaaResponse::MatrixError(RumaError {
|
let error = Error::RumaError(RumaError {
|
||||||
status_code: result.status(),
|
status_code: result.status(),
|
||||||
body: ErrorBody::Standard {
|
body: ErrorBody::Standard {
|
||||||
kind: ErrorKind::Unrecognized,
|
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) {
|
fn handle_result_log(method: &Method, uri: &Uri, result: &axum::response::Response) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue