From d3516495ab792ea436822520ae2f97e94c262dd4 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 5 May 2026 23:48:05 +0900 Subject: [PATCH] data_ingestion/jmdict: throw proper errors on invalid xrefs --- lib/_data_ingestion/jmdict/seed_data.dart | 47 ++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/_data_ingestion/jmdict/seed_data.dart b/lib/_data_ingestion/jmdict/seed_data.dart index 3f8a510..ef07fd5 100644 --- a/lib/_data_ingestion/jmdict/seed_data.dart +++ b/lib/_data_ingestion/jmdict/seed_data.dart @@ -27,15 +27,44 @@ ResolvedXref resolveXref( SplayTreeMap> entriesByReading, XRefParts xref, ) { - List candidateEntries = switch ((xref.kanjiRef, xref.readingRef)) { - (null, null) => throw Exception( - 'Xref $xref has no kanji or reading reference', - ), - (final String k, null) => entriesByKanji[k]!.toList(), - (null, final String r) => entriesByReading[r]!.toList(), - (final String k, final String r) => - entriesByKanji[k]!.intersection(entriesByReading[r]!).toList(), - }; + late List candidateEntries; + switch ((xref.kanjiRef, xref.readingRef)) { + case (null, null): + throw Exception('Xref $xref has no kanji or reading reference'); + + case (final String k, null): + if (!entriesByKanji.containsKey(k)) { + throw Exception( + 'Xref $xref has kanji reference "$k" but no entries found with that kanji', + ); + } + candidateEntries = entriesByKanji[k]!.toList(); + break; + + case (null, final String r): + if (!entriesByReading.containsKey(r)) { + throw Exception( + 'Xref $xref has reading reference "$r" but no entries found with that reading', + ); + } + candidateEntries = entriesByReading[r]!.toList(); + break; + + case (final String k, final String r): + if (!entriesByKanji.containsKey(k)) { + throw Exception( + 'Xref $xref has kanji reference "$k" but no entries found with that kanji', + ); + } + if (!entriesByReading.containsKey(r)) { + throw Exception( + 'Xref $xref has reading reference "$r" but no entries found with that reading', + ); + } + candidateEntries = entriesByKanji[k]! + .intersection(entriesByReading[r]!) + .toList(); + } // Filter out entries that don't have the number of senses specified in the xref if (xref.senseOrderNum != null) {