From 5a7bb1e8f104b66e65a16e447d930b478640383a Mon Sep 17 00:00:00 2001 From: Val Lorentz Date: Sat, 2 Dec 2023 17:51:19 +0100 Subject: [PATCH] Return error instead of panic when first event is not m.room.create --- src/service/rooms/timeline/mod.rs | 33 ++++++++++++++++++++----------- src/utils/error.rs | 2 ++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index a6e9f87a..06deb069 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -696,18 +696,27 @@ impl Service { .collect(); // If there was no create event yet, assume we are creating a room - let room_version_id = self.get_room_version(room_id)?.unwrap_or_else(|| { - if event_type != TimelineEventType::RoomCreate { - panic!("non-create event for room of unknown version"); - } - #[derive(Deserialize)] - struct RoomCreate { - room_version: RoomVersionId, - } - let content = serde_json::from_str::(content.get()) - .expect("Invalid content in RoomCreate pdu."); - content.room_version - }); + let room_version_id = self + .get_room_version(room_id)? + .or_else(|| { + if event_type == TimelineEventType::RoomCreate { + #[derive(Deserialize)] + struct RoomCreate { + room_version: RoomVersionId, + } + let content = serde_json::from_str::(content.get()) + .expect("Invalid content in RoomCreate pdu."); + Some(content.room_version) + } else { + None + } + }) + .ok_or_else(|| { + Error::InconsistentRoomState( + "non-create event for room of unknown version", + room_id.to_owned(), + ) + })?; let room_version = RoomVersion::new(&room_version_id).expect("room version is supported"); diff --git a/src/utils/error.rs b/src/utils/error.rs index 83ef3095..568ad8a0 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -82,6 +82,8 @@ pub enum Error { PathError(#[from] axum::extract::rejection::PathRejection), #[error("from {0}: {1}")] RedactionError(OwnedServerName, ruma::canonical_json::RedactionError), + #[error("{0} in {1}")] + InconsistentRoomState(&'static str, ruma::OwnedRoomId), } impl Error {