elaborate error macro and apply at various callsites

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-13 21:11:05 +00:00
parent b3f2288d07
commit 05efd9b044
23 changed files with 161 additions and 140 deletions

View file

@ -10,7 +10,7 @@ use std::{
};
use conduit::{
debug, debug_error, debug_info, error, info, trace,
debug, debug_error, debug_info, err, error, info, trace,
utils::{math::continue_exponential_backoff_secs, MutexMap},
warn, Error, Result,
};
@ -1382,11 +1382,8 @@ impl Service {
}
fn get_room_version_id(create_event: &PduEvent) -> Result<RoomVersionId> {
let create_event_content: RoomCreateEventContent =
serde_json::from_str(create_event.content.get()).map_err(|e| {
error!("Invalid create event: {}", e);
Error::BadDatabase("Invalid create event in db")
})?;
let create_event_content: RoomCreateEventContent = serde_json::from_str(create_event.content.get())
.map_err(|e| err!(Database("Invalid create event: {e}")))?;
Ok(create_event_content.room_version)
}

View file

@ -1,4 +1,4 @@
use conduit::{Error, Result};
use conduit::{Err, Error, Result};
use ruma::{api::client::error::ErrorKind, CanonicalJsonObject, OwnedEventId, OwnedRoomId, RoomId};
use serde_json::value::RawValue as RawJsonValue;
use tracing::warn;
@ -17,15 +17,12 @@ pub fn parse_incoming_pdu(pdu: &RawJsonValue) -> Result<(OwnedEventId, Canonical
.ok_or(Error::BadRequest(ErrorKind::InvalidParam, "Invalid room id in pdu"))?;
let Ok(room_version_id) = services().rooms.state.get_room_version(&room_id) else {
return Err(Error::Err(format!("Server is not in room {room_id}")));
return Err!("Server is not in room {room_id}");
};
let Ok((event_id, value)) = gen_event_id_canonical_json(pdu, &room_version_id) else {
// Event could not be converted to canonical json
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Could not convert event to canonical json.",
));
return Err!(Request(InvalidParam("Could not convert event to canonical json.")));
};
Ok((event_id, value, room_id))