Merge branch 'federation-errors' into 'master'

feat: forward federation errors to the client

See merge request famedly/conduit!85
next
Timo Kösters 2021-05-23 18:27:05 +00:00
commit ceaeab6816
3 changed files with 35 additions and 15 deletions

View File

@ -1,5 +1,11 @@
use log::{error, warn}; use log::{error, warn};
use ruma::api::client::{error::ErrorKind, r0::uiaa::UiaaInfo}; use ruma::{
api::client::{
error::{Error as RumaError, ErrorKind},
r0::uiaa::UiaaInfo,
},
ServerName,
};
use thiserror::Error; use thiserror::Error;
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
@ -10,7 +16,7 @@ use {
response::{self, Responder}, response::{self, Responder},
Request, Request,
}, },
ruma::api::client::{error::Error as RumaError, r0::uiaa::UiaaResponse}, ruma::api::client::r0::uiaa::UiaaResponse,
}; };
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -33,6 +39,8 @@ pub enum Error {
source: reqwest::Error, source: reqwest::Error,
}, },
#[error("{0}")] #[error("{0}")]
FederationError(Box<ServerName>, RumaError),
#[error("{0}")]
BadServerResponse(&'static str), BadServerResponse(&'static str),
#[error("{0}")] #[error("{0}")]
BadConfig(&'static str), BadConfig(&'static str),
@ -66,8 +74,13 @@ where
'o: 'r, 'o: 'r,
{ {
fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> { fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> {
if let Self::Uiaa(uiaainfo) = &self { if let Self::Uiaa(uiaainfo) = self {
return RumaResponse::from(UiaaResponse::AuthResponse(uiaainfo.clone())).respond_to(r); return RumaResponse::from(UiaaResponse::AuthResponse(uiaainfo)).respond_to(r);
}
if let Self::FederationError(origin, mut error) = self {
error.message = format!("Answer from {}: {}", origin, error.message);
return RumaResponse::from(error).respond_to(r);
} }
let message = format!("{}", self); let message = format!("{}", self);

View File

@ -137,9 +137,7 @@ where
let x_matrix = match request let x_matrix = match request
.headers() .headers()
.get_one("Authorization") .get_one("Authorization")
.and_then(|s| .and_then(|s| s.get(9..)) // Split off "X-Matrix " and parse the rest
// Split off "X-Matrix " and parse the rest
s.get(9..))
.map(|s| { .map(|s| {
s.split_terminator(',') s.split_terminator(',')
.map(|field| { .map(|field| {

View File

@ -9,7 +9,7 @@ use regex::Regex;
use rocket::{response::content::Json, State}; use rocket::{response::content::Json, State};
use ruma::{ use ruma::{
api::{ api::{
client::error::ErrorKind, client::error::{Error as RumaError, ErrorKind},
federation::{ federation::{
device::get_devices::{self, v1::UserDevice}, device::get_devices::{self, v1::UserDevice},
directory::{get_public_rooms, get_public_rooms_filtered}, directory::{get_public_rooms, get_public_rooms_filtered},
@ -27,7 +27,7 @@ use ruma::{
query::{get_profile_information, get_room_information}, query::{get_profile_information, get_room_information},
transactions::{edu::Edu, send_transaction_message}, transactions::{edu::Edu, send_transaction_message},
}, },
IncomingResponse, OutgoingRequest, OutgoingResponse, SendAccessToken, EndpointError, IncomingResponse, OutgoingRequest, OutgoingResponse, SendAccessToken,
}, },
directory::{IncomingFilter, IncomingRoomNetwork}, directory::{IncomingFilter, IncomingRoomNetwork},
events::{ events::{
@ -261,12 +261,21 @@ where
); );
} }
let response = T::IncomingResponse::try_from_http_response( let http_response = http_response_builder
http_response_builder
.body(body) .body(body)
.expect("reqwest body is valid http body"), .expect("reqwest body is valid http body");
);
response.map_err(|_| Error::BadServerResponse("Server returned bad response.")) if status == 200 {
let response = T::IncomingResponse::try_from_http_response(http_response);
response.map_err(|_| Error::BadServerResponse("Server returned bad 200 response."))
} else {
Err(Error::FederationError(
destination.to_owned(),
RumaError::try_from_http_response(http_response).map_err(|_| {
Error::BadServerResponse("Server returned bad error response.")
})?,
))
}
} }
Err(e) => Err(e.into()), Err(e) => Err(e.into()),
} }