From e936d183244ca1dd8d2e01cf9240935d2de55360 Mon Sep 17 00:00:00 2001 From: timedout Date: Mon, 29 Jun 2026 18:12:16 +0100 Subject: [PATCH] feat: Don't panic in `build_local_dag` Previously the function assumed the caller had performed proper validation on the inputs (and all current callers do), but this is a poor reason to panic when sane error handling is available. Events with no prev events now return an error, and prev events which are illegal are simply skipped. --- .../rooms/event_handler/fetch_and_handle_outliers.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs index 53c7b4da6..41104378f 100644 --- a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs +++ b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs @@ -54,11 +54,13 @@ pub async fn build_local_dag( let prev_events = value .get("prev_events") .and_then(CanonicalJsonValue::as_array) - .expect("prev_events must be present in PDU JSON object") + .ok_or_else(|| { + err!(Request(BadJson("event JSON for {event_id} is missing prev_events"))) + })? .iter() - .map(|v| v.as_str().expect("prev_events entries must be strings")) - .map(|v| EventId::parse(v).expect("prev_events entries must be valid event IDs")) - .filter(|id| pdu_map.contains_key(id)) + .map(|v| v.as_str().and_then(|s| EventId::parse(s).ok())) + .filter(|id| id.as_ref().is_some_and(|id| pdu_map.contains_key(id))) + .map(Option::unwrap) .collect(); dag.insert(event_id.clone(), prev_events);