1#[cfg(test)]
9mod tests;
10
11use std::fmt;
12use thiserror::Error as ThisError;
13
14#[derive(Debug, ThisError)]
115#[error("{message}")]
116pub struct InternalError {
117 pub(crate) class: ErrorClass,
118 pub(crate) origin: ErrorOrigin,
119 pub(crate) message: String,
120
121 pub(crate) detail: Option<ErrorDetail>,
124}
125
126impl InternalError {
127 #[cold]
131 #[inline(never)]
132 pub fn new(class: ErrorClass, origin: ErrorOrigin, message: impl Into<String>) -> Self {
133 let message = message.into();
134
135 let detail = match (class, origin) {
136 (ErrorClass::Corruption, ErrorOrigin::Store) => {
137 Some(ErrorDetail::Store(StoreError::Corrupt {
138 message: message.clone(),
139 }))
140 }
141 (ErrorClass::InvariantViolation, ErrorOrigin::Store) => {
142 Some(ErrorDetail::Store(StoreError::InvariantViolation {
143 message: message.clone(),
144 }))
145 }
146 _ => None,
147 };
148
149 Self {
150 class,
151 origin,
152 message,
153 detail,
154 }
155 }
156
157 #[must_use]
159 pub const fn class(&self) -> ErrorClass {
160 self.class
161 }
162
163 #[must_use]
165 pub const fn origin(&self) -> ErrorOrigin {
166 self.origin
167 }
168
169 #[must_use]
171 pub fn message(&self) -> &str {
172 &self.message
173 }
174
175 #[must_use]
177 pub const fn detail(&self) -> Option<&ErrorDetail> {
178 self.detail.as_ref()
179 }
180
181 #[must_use]
183 pub fn into_message(self) -> String {
184 self.message
185 }
186
187 #[cold]
189 #[inline(never)]
190 pub(crate) fn classified(
191 class: ErrorClass,
192 origin: ErrorOrigin,
193 message: impl Into<String>,
194 ) -> Self {
195 Self::new(class, origin, message)
196 }
197
198 #[cold]
200 #[inline(never)]
201 pub(crate) fn with_message(self, message: impl Into<String>) -> Self {
202 Self::classified(self.class, self.origin, message)
203 }
204
205 #[cold]
209 #[inline(never)]
210 pub(crate) fn with_origin(self, origin: ErrorOrigin) -> Self {
211 Self::classified(self.class, origin, self.message)
212 }
213
214 #[cold]
216 #[inline(never)]
217 pub(crate) fn index_invariant(message: impl Into<String>) -> Self {
218 Self::new(
219 ErrorClass::InvariantViolation,
220 ErrorOrigin::Index,
221 message.into(),
222 )
223 }
224
225 pub(crate) fn index_key_field_count_exceeds_max(
227 index_name: &str,
228 field_count: usize,
229 max_fields: usize,
230 ) -> Self {
231 Self::index_invariant(format!(
232 "index '{index_name}' has {field_count} fields (max {max_fields})",
233 ))
234 }
235
236 pub(crate) fn index_key_item_field_missing_on_entity_model(field: &str) -> Self {
238 Self::index_invariant(format!(
239 "index key item field missing on entity model: {field}",
240 ))
241 }
242
243 pub(crate) fn index_key_item_field_missing_on_lookup_row(field: &str) -> Self {
245 Self::index_invariant(format!(
246 "index key item field missing on lookup row: {field}",
247 ))
248 }
249
250 pub(crate) fn index_expression_source_type_mismatch(
252 index_name: &str,
253 expression: impl fmt::Display,
254 expected: &str,
255 source_label: &str,
256 ) -> Self {
257 Self::index_invariant(format!(
258 "index '{index_name}' expression '{expression}' expected {expected} source value, got {source_label}",
259 ))
260 }
261
262 #[cold]
265 #[inline(never)]
266 pub(crate) fn planner_executor_invariant(reason: impl Into<String>) -> Self {
267 Self::new(
268 ErrorClass::InvariantViolation,
269 ErrorOrigin::Planner,
270 Self::executor_invariant_message(reason),
271 )
272 }
273
274 #[cold]
277 #[inline(never)]
278 pub(crate) fn query_executor_invariant(reason: impl Into<String>) -> Self {
279 Self::new(
280 ErrorClass::InvariantViolation,
281 ErrorOrigin::Query,
282 Self::executor_invariant_message(reason),
283 )
284 }
285
286 #[cold]
289 #[inline(never)]
290 pub(crate) fn cursor_executor_invariant(reason: impl Into<String>) -> Self {
291 Self::new(
292 ErrorClass::InvariantViolation,
293 ErrorOrigin::Cursor,
294 Self::executor_invariant_message(reason),
295 )
296 }
297
298 #[cold]
300 #[inline(never)]
301 pub(crate) fn executor_invariant(message: impl Into<String>) -> Self {
302 Self::new(
303 ErrorClass::InvariantViolation,
304 ErrorOrigin::Executor,
305 message.into(),
306 )
307 }
308
309 #[cold]
311 #[inline(never)]
312 pub(crate) fn executor_internal(message: impl Into<String>) -> Self {
313 Self::new(ErrorClass::Internal, ErrorOrigin::Executor, message.into())
314 }
315
316 #[cold]
318 #[inline(never)]
319 pub(crate) fn executor_unsupported(message: impl Into<String>) -> Self {
320 Self::new(
321 ErrorClass::Unsupported,
322 ErrorOrigin::Executor,
323 message.into(),
324 )
325 }
326
327 pub(crate) fn mutation_entity_primary_key_missing(entity_path: &str, field_name: &str) -> Self {
329 Self::executor_invariant(format!(
330 "entity primary key field missing: {entity_path} field={field_name}",
331 ))
332 }
333
334 pub(crate) fn mutation_entity_primary_key_invalid_value(
336 entity_path: &str,
337 field_name: &str,
338 value: &crate::value::Value,
339 ) -> Self {
340 Self::executor_invariant(format!(
341 "entity primary key field has invalid value: {entity_path} field={field_name} value={value:?}",
342 ))
343 }
344
345 pub(crate) fn mutation_entity_primary_key_type_mismatch(
347 entity_path: &str,
348 field_name: &str,
349 value: &crate::value::Value,
350 ) -> Self {
351 Self::executor_invariant(format!(
352 "entity primary key field type mismatch: {entity_path} field={field_name} value={value:?}",
353 ))
354 }
355
356 pub(crate) fn mutation_entity_primary_key_mismatch(
358 entity_path: &str,
359 field_name: &str,
360 field_value: &crate::value::Value,
361 identity_key: &crate::value::Value,
362 ) -> Self {
363 Self::executor_invariant(format!(
364 "entity primary key mismatch: {entity_path} field={field_name} field_value={field_value:?} id_key={identity_key:?}",
365 ))
366 }
367
368 pub(crate) fn mutation_entity_field_missing(
370 entity_path: &str,
371 field_name: &str,
372 indexed: bool,
373 ) -> Self {
374 let indexed_note = if indexed { " (indexed)" } else { "" };
375
376 Self::executor_invariant(format!(
377 "entity field missing: {entity_path} field={field_name}{indexed_note}",
378 ))
379 }
380
381 pub(crate) fn mutation_structural_patch_required_field_missing(
383 entity_path: &str,
384 field_name: &str,
385 ) -> Self {
386 Self::executor_invariant(format!(
387 "structural patch missing required field: {entity_path} field={field_name}",
388 ))
389 }
390
391 pub(crate) fn mutation_entity_field_type_mismatch(
393 entity_path: &str,
394 field_name: &str,
395 value: &crate::value::Value,
396 ) -> Self {
397 Self::executor_invariant(format!(
398 "entity field type mismatch: {entity_path} field={field_name} value={value:?}",
399 ))
400 }
401
402 pub(crate) fn mutation_generated_field_explicit(entity_path: &str, field_name: &str) -> Self {
404 Self::executor_unsupported(format!(
405 "generated field may not be explicitly written: {entity_path} field={field_name}",
406 ))
407 }
408
409 #[must_use]
411 pub fn mutation_create_missing_authored_fields(entity_path: &str, field_names: &str) -> Self {
412 Self::executor_unsupported(format!(
413 "create requires explicit values for authorable fields {field_names}: {entity_path}",
414 ))
415 }
416
417 pub(crate) fn mutation_structural_after_image_invalid(
422 entity_path: &str,
423 data_key: impl fmt::Display,
424 detail: impl AsRef<str>,
425 ) -> Self {
426 Self::executor_invariant(format!(
427 "mutation result is invalid: {entity_path} key={data_key} ({})",
428 detail.as_ref(),
429 ))
430 }
431
432 pub(crate) fn mutation_structural_field_unknown(entity_path: &str, field_name: &str) -> Self {
434 Self::executor_invariant(format!(
435 "mutation field not found: {entity_path} field={field_name}",
436 ))
437 }
438
439 pub(crate) fn mutation_decimal_scale_mismatch(
441 entity_path: &str,
442 field_name: &str,
443 expected_scale: impl fmt::Display,
444 actual_scale: impl fmt::Display,
445 ) -> Self {
446 Self::executor_unsupported(format!(
447 "decimal field scale mismatch: {entity_path} field={field_name} expected_scale={expected_scale} actual_scale={actual_scale}",
448 ))
449 }
450
451 pub(crate) fn mutation_text_max_len_exceeded(
453 entity_path: &str,
454 field_name: &str,
455 max_len: impl fmt::Display,
456 actual_len: impl fmt::Display,
457 ) -> Self {
458 Self::executor_unsupported(format!(
459 "text length exceeds max_len: {entity_path} field={field_name} max_len={max_len} actual_len={actual_len}",
460 ))
461 }
462
463 pub(crate) fn mutation_set_field_list_required(entity_path: &str, field_name: &str) -> Self {
465 Self::executor_invariant(format!(
466 "set field must encode as Value::List: {entity_path} field={field_name}",
467 ))
468 }
469
470 pub(crate) fn mutation_set_field_not_canonical(entity_path: &str, field_name: &str) -> Self {
472 Self::executor_invariant(format!(
473 "set field must be strictly ordered and deduplicated: {entity_path} field={field_name}",
474 ))
475 }
476
477 pub(crate) fn mutation_map_field_map_required(entity_path: &str, field_name: &str) -> Self {
479 Self::executor_invariant(format!(
480 "map field must encode as Value::Map: {entity_path} field={field_name}",
481 ))
482 }
483
484 pub(crate) fn mutation_map_field_entries_invalid(
486 entity_path: &str,
487 field_name: &str,
488 detail: impl fmt::Display,
489 ) -> Self {
490 Self::executor_invariant(format!(
491 "map field entries violate map invariants: {entity_path} field={field_name} ({detail})",
492 ))
493 }
494
495 pub(crate) fn mutation_map_field_entries_not_canonical(
497 entity_path: &str,
498 field_name: &str,
499 ) -> Self {
500 Self::executor_invariant(format!(
501 "map field entries are not in canonical deterministic order: {entity_path} field={field_name}",
502 ))
503 }
504
505 pub(crate) fn scalar_page_ordering_after_filtering_required() -> Self {
507 Self::query_executor_invariant("ordering must run after filtering")
508 }
509
510 pub(crate) fn scalar_page_cursor_boundary_order_required() -> Self {
512 Self::query_executor_invariant("cursor boundary requires ordering")
513 }
514
515 pub(crate) fn scalar_page_cursor_boundary_after_ordering_required() -> Self {
517 Self::query_executor_invariant("cursor boundary must run after ordering")
518 }
519
520 pub(crate) fn scalar_page_pagination_after_ordering_required() -> Self {
522 Self::query_executor_invariant("pagination must run after ordering")
523 }
524
525 pub(crate) fn scalar_page_delete_limit_after_ordering_required() -> Self {
527 Self::query_executor_invariant("delete limit must run after ordering")
528 }
529
530 pub(crate) fn load_runtime_scalar_payload_required() -> Self {
532 Self::query_executor_invariant("scalar load mode must carry scalar runtime payload")
533 }
534
535 pub(crate) fn load_runtime_grouped_payload_required() -> Self {
537 Self::query_executor_invariant("grouped load mode must carry grouped runtime payload")
538 }
539
540 pub(crate) fn load_runtime_scalar_surface_payload_required() -> Self {
542 Self::query_executor_invariant("scalar page load mode must carry scalar runtime payload")
543 }
544
545 pub(crate) fn load_runtime_grouped_surface_payload_required() -> Self {
547 Self::query_executor_invariant("grouped page load mode must carry grouped runtime payload")
548 }
549
550 pub(crate) fn load_executor_load_plan_required() -> Self {
552 Self::query_executor_invariant("load executor requires load plans")
553 }
554
555 pub(crate) fn delete_executor_grouped_unsupported() -> Self {
557 Self::executor_unsupported("grouped query execution is not yet enabled in this release")
558 }
559
560 pub(crate) fn delete_executor_delete_plan_required() -> Self {
562 Self::query_executor_invariant("delete executor requires delete plans")
563 }
564
565 pub(crate) fn aggregate_fold_mode_terminal_contract_required() -> Self {
567 Self::query_executor_invariant(
568 "aggregate fold mode must match route fold-mode contract for aggregate terminal",
569 )
570 }
571
572 pub(crate) fn fast_stream_route_kind_request_match_required() -> Self {
574 Self::query_executor_invariant("fast-stream route kind/request mismatch")
575 }
576
577 pub(crate) fn secondary_index_prefix_spec_required() -> Self {
579 Self::query_executor_invariant(
580 "index-prefix executable spec must be materialized for index-prefix plans",
581 )
582 }
583
584 pub(crate) fn index_range_limit_spec_required() -> Self {
586 Self::query_executor_invariant(
587 "index-range executable spec must be materialized for index-range plans",
588 )
589 }
590
591 pub(crate) fn mutation_atomic_save_duplicate_key(
593 entity_path: &str,
594 key: impl fmt::Display,
595 ) -> Self {
596 Self::executor_unsupported(format!(
597 "atomic save batch rejected duplicate key: entity={entity_path} key={key}",
598 ))
599 }
600
601 pub(crate) fn mutation_index_store_generation_changed(
603 expected_generation: u64,
604 observed_generation: u64,
605 ) -> Self {
606 Self::executor_invariant(format!(
607 "index store generation changed between preflight and apply: expected {expected_generation}, found {observed_generation}",
608 ))
609 }
610
611 #[must_use]
613 #[cold]
614 #[inline(never)]
615 pub(crate) fn executor_invariant_message(reason: impl Into<String>) -> String {
616 format!("executor invariant violated: {}", reason.into())
617 }
618
619 #[cold]
621 #[inline(never)]
622 pub(crate) fn planner_invariant(message: impl Into<String>) -> Self {
623 Self::new(
624 ErrorClass::InvariantViolation,
625 ErrorOrigin::Planner,
626 message.into(),
627 )
628 }
629
630 #[must_use]
632 pub(crate) fn invalid_logical_plan_message(reason: impl Into<String>) -> String {
633 format!("invalid logical plan: {}", reason.into())
634 }
635
636 pub(crate) fn query_invalid_logical_plan(reason: impl Into<String>) -> Self {
638 Self::planner_invariant(Self::invalid_logical_plan_message(reason))
639 }
640
641 pub(crate) fn store_invariant(message: impl Into<String>) -> Self {
643 Self::new(
644 ErrorClass::InvariantViolation,
645 ErrorOrigin::Store,
646 message.into(),
647 )
648 }
649
650 pub(crate) fn duplicate_runtime_hooks_for_entity_tag(
652 entity_tag: crate::types::EntityTag,
653 ) -> Self {
654 Self::store_invariant(format!(
655 "duplicate runtime hooks for entity tag '{}'",
656 entity_tag.value()
657 ))
658 }
659
660 pub(crate) fn duplicate_runtime_hooks_for_entity_path(entity_path: &str) -> Self {
662 Self::store_invariant(format!(
663 "duplicate runtime hooks for entity path '{entity_path}'"
664 ))
665 }
666
667 #[cold]
669 #[inline(never)]
670 pub(crate) fn store_internal(message: impl Into<String>) -> Self {
671 Self::new(ErrorClass::Internal, ErrorOrigin::Store, message.into())
672 }
673
674 pub(crate) fn commit_memory_id_unconfigured() -> Self {
676 Self::store_internal(
677 "commit memory id is not configured; initialize recovery before commit store access",
678 )
679 }
680
681 pub(crate) fn commit_memory_id_mismatch(cached_id: u8, configured_id: u8) -> Self {
683 Self::store_internal(format!(
684 "commit memory id mismatch: cached={cached_id}, configured={configured_id}",
685 ))
686 }
687
688 pub(crate) fn commit_memory_stable_key_mismatch(
690 cached_key: &str,
691 configured_key: &str,
692 ) -> Self {
693 Self::store_internal(format!(
694 "commit memory stable key mismatch: cached={cached_key}, configured={configured_key}",
695 ))
696 }
697
698 pub(crate) fn delete_rollback_row_required() -> Self {
700 Self::store_internal("missing raw row for delete rollback")
701 }
702
703 pub(crate) fn recovery_integrity_validation_failed(
705 missing_index_entries: u64,
706 divergent_index_entries: u64,
707 orphan_index_references: u64,
708 ) -> Self {
709 Self::store_corruption(format!(
710 "recovery integrity validation failed: missing_index_entries={missing_index_entries} divergent_index_entries={divergent_index_entries} orphan_index_references={orphan_index_references}",
711 ))
712 }
713
714 #[cold]
716 #[inline(never)]
717 pub(crate) fn index_internal(message: impl Into<String>) -> Self {
718 Self::new(ErrorClass::Internal, ErrorOrigin::Index, message.into())
719 }
720
721 pub(crate) fn structural_index_removal_entity_key_required() -> Self {
723 Self::index_internal("missing old entity key for structural index removal")
724 }
725
726 pub(crate) fn structural_index_insertion_entity_key_required() -> Self {
728 Self::index_internal("missing new entity key for structural index insertion")
729 }
730
731 pub(crate) fn index_commit_op_old_entity_key_required() -> Self {
733 Self::index_internal("missing old entity key for index removal")
734 }
735
736 pub(crate) fn index_commit_op_new_entity_key_required() -> Self {
738 Self::index_internal("missing new entity key for index insertion")
739 }
740
741 #[cfg(test)]
743 pub(crate) fn query_internal(message: impl Into<String>) -> Self {
744 Self::new(ErrorClass::Internal, ErrorOrigin::Query, message.into())
745 }
746
747 #[cold]
749 #[inline(never)]
750 pub(crate) fn query_unsupported(message: impl Into<String>) -> Self {
751 Self::new(ErrorClass::Unsupported, ErrorOrigin::Query, message.into())
752 }
753
754 #[cold]
756 #[inline(never)]
757 pub(crate) fn query_numeric_overflow() -> Self {
758 Self {
759 class: ErrorClass::Unsupported,
760 origin: ErrorOrigin::Query,
761 message: "numeric overflow".to_string(),
762 detail: Some(ErrorDetail::Query(QueryErrorDetail::NumericOverflow)),
763 }
764 }
765
766 #[cold]
769 #[inline(never)]
770 pub(crate) fn query_numeric_not_representable() -> Self {
771 Self {
772 class: ErrorClass::Unsupported,
773 origin: ErrorOrigin::Query,
774 message: "numeric result is not representable".to_string(),
775 detail: Some(ErrorDetail::Query(
776 QueryErrorDetail::NumericNotRepresentable,
777 )),
778 }
779 }
780
781 #[cold]
783 #[inline(never)]
784 pub(crate) fn serialize_internal(message: impl Into<String>) -> Self {
785 Self::new(ErrorClass::Internal, ErrorOrigin::Serialize, message.into())
786 }
787
788 pub(crate) fn persisted_row_encode_failed(detail: impl fmt::Display) -> Self {
790 Self::serialize_internal(format!("row encode failed: {detail}"))
791 }
792
793 pub(crate) fn persisted_row_field_encode_failed(
795 field_name: &str,
796 detail: impl fmt::Display,
797 ) -> Self {
798 Self::serialize_internal(format!(
799 "row encode failed for field '{field_name}': {detail}",
800 ))
801 }
802
803 pub(crate) fn bytes_field_value_encode_failed(detail: impl fmt::Display) -> Self {
805 Self::serialize_internal(format!("bytes(field) value encode failed: {detail}"))
806 }
807
808 #[cold]
810 #[inline(never)]
811 pub(crate) fn store_corruption(message: impl Into<String>) -> Self {
812 Self::new(ErrorClass::Corruption, ErrorOrigin::Store, message.into())
813 }
814
815 pub(crate) fn commit_corruption(detail: impl fmt::Display) -> Self {
817 Self::store_corruption(format!("commit marker corrupted: {detail}"))
818 }
819
820 pub(crate) fn commit_component_corruption(component: &str, detail: impl fmt::Display) -> Self {
822 Self::store_corruption(format!("commit marker {component} corrupted: {detail}"))
823 }
824
825 pub(crate) fn commit_id_generation_failed(detail: impl fmt::Display) -> Self {
827 Self::store_internal(format!("commit id generation failed: {detail}"))
828 }
829
830 pub(crate) fn commit_marker_payload_exceeds_u32_length_limit(label: &str, len: usize) -> Self {
832 Self::store_unsupported(format!("{label} exceeds u32 length limit: {len} bytes"))
833 }
834
835 pub(crate) fn commit_component_length_invalid(
837 component: &str,
838 len: usize,
839 expected: impl fmt::Display,
840 ) -> Self {
841 Self::commit_component_corruption(
842 component,
843 format!("invalid length {len}, expected {expected}"),
844 )
845 }
846
847 pub(crate) fn commit_marker_exceeds_max_size(size: usize, max_size: u32) -> Self {
849 Self::commit_corruption(format!(
850 "commit marker exceeds max size: {size} bytes (limit {max_size})",
851 ))
852 }
853
854 #[cfg(test)]
856 pub(crate) fn commit_marker_exceeds_max_size_before_persist(
857 size: usize,
858 max_size: u32,
859 ) -> Self {
860 Self::store_unsupported(format!(
861 "commit marker exceeds max size: {size} bytes (limit {max_size})",
862 ))
863 }
864
865 pub(crate) fn commit_control_slot_exceeds_max_size(size: usize, max_size: u32) -> Self {
867 Self::store_unsupported(format!(
868 "commit control slot exceeds max size: {size} bytes (limit {max_size})",
869 ))
870 }
871
872 pub(crate) fn commit_control_slot_marker_bytes_exceed_u32_length_limit(size: usize) -> Self {
874 Self::store_unsupported(format!(
875 "commit marker bytes exceed u32 length limit: {size} bytes",
876 ))
877 }
878
879 pub(crate) fn startup_index_rebuild_invalid_data_key(
881 store_path: &str,
882 detail: impl fmt::Display,
883 ) -> Self {
884 Self::store_corruption(format!(
885 "startup index rebuild failed: invalid data key in store '{store_path}' ({detail})",
886 ))
887 }
888
889 #[cold]
891 #[inline(never)]
892 pub(crate) fn index_corruption(message: impl Into<String>) -> Self {
893 Self::new(ErrorClass::Corruption, ErrorOrigin::Index, message.into())
894 }
895
896 pub(crate) fn index_unique_validation_corruption(
898 entity_path: &str,
899 fields: &str,
900 detail: impl fmt::Display,
901 ) -> Self {
902 Self::index_plan_index_corruption(format!(
903 "index corrupted: {entity_path} ({fields}) -> {detail}",
904 ))
905 }
906
907 pub(crate) fn structural_index_entry_corruption(
909 entity_path: &str,
910 fields: &str,
911 detail: impl fmt::Display,
912 ) -> Self {
913 Self::index_plan_index_corruption(format!(
914 "index corrupted: {entity_path} ({fields}) -> {detail}",
915 ))
916 }
917
918 pub(crate) fn index_unique_validation_entity_key_required() -> Self {
920 Self::index_invariant("missing entity key during unique validation")
921 }
922
923 pub(crate) fn index_unique_validation_row_deserialize_failed(
925 data_key: impl fmt::Display,
926 source: impl fmt::Display,
927 ) -> Self {
928 Self::index_plan_serialize_corruption(format!(
929 "failed to structurally deserialize row: {data_key} ({source})"
930 ))
931 }
932
933 pub(crate) fn index_unique_validation_primary_key_decode_failed(
935 data_key: impl fmt::Display,
936 source: impl fmt::Display,
937 ) -> Self {
938 Self::index_plan_serialize_corruption(format!(
939 "failed to decode structural primary-key slot: {data_key} ({source})"
940 ))
941 }
942
943 pub(crate) fn index_unique_validation_key_rebuild_failed(
945 data_key: impl fmt::Display,
946 entity_path: &str,
947 source: impl fmt::Display,
948 ) -> Self {
949 Self::index_plan_serialize_corruption(format!(
950 "failed to structurally decode unique key row {data_key} for {entity_path}: {source}",
951 ))
952 }
953
954 pub(crate) fn index_unique_validation_row_required(data_key: impl fmt::Display) -> Self {
956 Self::index_plan_store_corruption(format!("missing row: {data_key}"))
957 }
958
959 pub(crate) fn index_only_predicate_component_required() -> Self {
961 Self::index_invariant("index-only predicate program referenced missing index component")
962 }
963
964 pub(crate) fn index_scan_continuation_anchor_within_envelope_required() -> Self {
966 Self::index_invariant(
967 "index-range continuation anchor is outside the requested range envelope",
968 )
969 }
970
971 pub(crate) fn index_scan_continuation_advancement_required() -> Self {
973 Self::index_invariant("index-range continuation scan did not advance beyond the anchor")
974 }
975
976 pub(crate) fn index_scan_key_corrupted_during(
978 context: &'static str,
979 err: impl fmt::Display,
980 ) -> Self {
981 Self::index_corruption(format!("index key corrupted during {context}: {err}"))
982 }
983
984 pub(crate) fn index_projection_component_required(
986 index_name: &str,
987 component_index: usize,
988 ) -> Self {
989 Self::index_invariant(format!(
990 "index projection referenced missing component: index='{index_name}' component_index={component_index}",
991 ))
992 }
993
994 pub(crate) fn index_entry_decode_failed(err: impl fmt::Display) -> Self {
996 Self::index_corruption(err.to_string())
997 }
998
999 pub(crate) fn serialize_corruption(message: impl Into<String>) -> Self {
1001 Self::new(
1002 ErrorClass::Corruption,
1003 ErrorOrigin::Serialize,
1004 message.into(),
1005 )
1006 }
1007
1008 pub(crate) fn persisted_row_decode_failed(detail: impl fmt::Display) -> Self {
1010 Self::serialize_corruption(format!("row decode: {detail}"))
1011 }
1012
1013 pub(crate) fn persisted_row_field_decode_failed(
1015 field_name: &str,
1016 detail: impl fmt::Display,
1017 ) -> Self {
1018 Self::serialize_corruption(format!(
1019 "row decode failed for field '{field_name}': {detail}",
1020 ))
1021 }
1022
1023 pub(crate) fn persisted_row_field_kind_decode_failed(
1025 field_name: &str,
1026 field_kind: impl fmt::Debug,
1027 detail: impl fmt::Display,
1028 ) -> Self {
1029 Self::persisted_row_field_decode_failed(
1030 field_name,
1031 format!("kind={field_kind:?}: {detail}"),
1032 )
1033 }
1034
1035 pub(crate) fn persisted_row_field_payload_exact_len_required(
1037 field_name: &str,
1038 payload_kind: &str,
1039 expected_len: usize,
1040 ) -> Self {
1041 let unit = if expected_len == 1 { "byte" } else { "bytes" };
1042
1043 Self::persisted_row_field_decode_failed(
1044 field_name,
1045 format!("{payload_kind} payload must be exactly {expected_len} {unit}"),
1046 )
1047 }
1048
1049 pub(crate) fn persisted_row_field_payload_must_be_empty(
1051 field_name: &str,
1052 payload_kind: &str,
1053 ) -> Self {
1054 Self::persisted_row_field_decode_failed(
1055 field_name,
1056 format!("{payload_kind} payload must be empty"),
1057 )
1058 }
1059
1060 pub(crate) fn persisted_row_field_payload_invalid_byte(
1062 field_name: &str,
1063 payload_kind: &str,
1064 value: u8,
1065 ) -> Self {
1066 Self::persisted_row_field_decode_failed(
1067 field_name,
1068 format!("invalid {payload_kind} payload byte {value}"),
1069 )
1070 }
1071
1072 pub(crate) fn persisted_row_field_payload_non_finite(
1074 field_name: &str,
1075 payload_kind: &str,
1076 ) -> Self {
1077 Self::persisted_row_field_decode_failed(
1078 field_name,
1079 format!("{payload_kind} payload is non-finite"),
1080 )
1081 }
1082
1083 pub(crate) fn persisted_row_field_payload_out_of_range(
1085 field_name: &str,
1086 payload_kind: &str,
1087 ) -> Self {
1088 Self::persisted_row_field_decode_failed(
1089 field_name,
1090 format!("{payload_kind} payload out of range for target type"),
1091 )
1092 }
1093
1094 pub(crate) fn persisted_row_field_text_payload_invalid_utf8(
1096 field_name: &str,
1097 detail: impl fmt::Display,
1098 ) -> Self {
1099 Self::persisted_row_field_decode_failed(
1100 field_name,
1101 format!("invalid UTF-8 text payload ({detail})"),
1102 )
1103 }
1104
1105 pub(crate) fn persisted_row_slot_lookup_out_of_bounds(model_path: &str, slot: usize) -> Self {
1107 Self::index_invariant(format!(
1108 "slot lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1109 ))
1110 }
1111
1112 pub(crate) fn persisted_row_slot_cache_lookup_out_of_bounds(
1114 model_path: &str,
1115 slot: usize,
1116 ) -> Self {
1117 Self::index_invariant(format!(
1118 "slot cache lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1119 ))
1120 }
1121
1122 pub(crate) fn persisted_row_primary_key_not_primary_key_encodable(
1124 data_key: impl fmt::Debug,
1125 detail: impl fmt::Display,
1126 ) -> Self {
1127 Self::persisted_row_decode_failed(format!(
1128 "primary-key value is not primary-key encodable: {data_key:?} ({detail})",
1129 ))
1130 }
1131
1132 pub(crate) fn persisted_row_primary_key_slot_missing(data_key: impl fmt::Debug) -> Self {
1134 Self::persisted_row_decode_failed(format!(
1135 "missing primary-key slot while validating {data_key:?}",
1136 ))
1137 }
1138
1139 pub(crate) fn persisted_row_key_mismatch(
1141 expected_key: impl fmt::Debug,
1142 found_key: impl fmt::Debug,
1143 ) -> Self {
1144 Self::store_corruption(format!(
1145 "row key mismatch: expected {expected_key:?}, found {found_key:?}",
1146 ))
1147 }
1148
1149 pub(crate) fn persisted_row_declared_field_missing(field_name: &str) -> Self {
1151 Self::persisted_row_decode_failed(format!("missing declared field `{field_name}`"))
1152 }
1153
1154 pub(crate) fn data_key_entity_mismatch(
1156 expected: impl fmt::Display,
1157 found: impl fmt::Display,
1158 ) -> Self {
1159 Self::store_corruption(format!(
1160 "data key entity mismatch: expected {expected}, found {found}",
1161 ))
1162 }
1163
1164 pub(crate) fn reverse_index_ordinal_overflow(
1166 source_path: &str,
1167 field_name: &str,
1168 target_path: &str,
1169 detail: impl fmt::Display,
1170 ) -> Self {
1171 Self::index_internal(format!(
1172 "reverse index ordinal overflow: source={source_path} field={field_name} target={target_path} ({detail})",
1173 ))
1174 }
1175
1176 pub(crate) fn reverse_index_entry_corrupted(
1178 source_path: &str,
1179 field_name: &str,
1180 target_path: &str,
1181 index_key: impl fmt::Debug,
1182 detail: impl fmt::Display,
1183 ) -> Self {
1184 Self::index_corruption(format!(
1185 "reverse index entry corrupted: source={source_path} field={field_name} target={target_path} key={index_key:?} ({detail})",
1186 ))
1187 }
1188
1189 pub(crate) fn relation_target_store_missing(
1191 source_path: &str,
1192 field_name: &str,
1193 target_path: &str,
1194 store_path: &str,
1195 detail: impl fmt::Display,
1196 ) -> Self {
1197 Self::executor_internal(format!(
1198 "relation target store missing: source={source_path} field={field_name} target={target_path} store={store_path} ({detail})",
1199 ))
1200 }
1201
1202 pub(crate) fn relation_target_key_decode_failed(
1204 context_label: &str,
1205 source_path: &str,
1206 field_name: &str,
1207 target_path: &str,
1208 detail: impl fmt::Display,
1209 ) -> Self {
1210 Self::identity_corruption(format!(
1211 "{context_label}: source={source_path} field={field_name} target={target_path} ({detail})",
1212 ))
1213 }
1214
1215 pub(crate) fn relation_target_entity_mismatch(
1217 context_label: &str,
1218 source_path: &str,
1219 field_name: &str,
1220 target_path: &str,
1221 target_entity_name: &str,
1222 expected_tag: impl fmt::Display,
1223 actual_tag: impl fmt::Display,
1224 ) -> Self {
1225 Self::store_corruption(format!(
1226 "{context_label}: source={source_path} field={field_name} target={target_path} expected={target_entity_name} (tag={expected_tag}) actual_tag={actual_tag}",
1227 ))
1228 }
1229
1230 pub(crate) fn relation_source_row_decode_failed(
1232 source_path: &str,
1233 field_name: &str,
1234 target_path: &str,
1235 detail: impl fmt::Display,
1236 ) -> Self {
1237 Self::serialize_corruption(format!(
1238 "relation source row decode: source={source_path} field={field_name} target={target_path} ({detail})",
1239 ))
1240 }
1241
1242 pub(crate) fn relation_source_row_unsupported_scalar_relation_key(
1244 source_path: &str,
1245 field_name: &str,
1246 target_path: &str,
1247 ) -> Self {
1248 Self::serialize_corruption(format!(
1249 "relation source row decode: unsupported scalar relation key: source={source_path} field={field_name} target={target_path}",
1250 ))
1251 }
1252
1253 pub(crate) fn relation_source_row_invalid_field_kind(field_kind: impl fmt::Debug) -> Self {
1255 Self::serialize_corruption(format!(
1256 "invalid strong relation field kind during structural decode: {field_kind:?}"
1257 ))
1258 }
1259
1260 pub(crate) fn relation_source_row_unsupported_key_kind(field_kind: impl fmt::Debug) -> Self {
1262 Self::serialize_corruption(format!(
1263 "unsupported strong relation key kind during structural decode: {field_kind:?}"
1264 ))
1265 }
1266
1267 pub(crate) fn reverse_index_relation_target_decode_invariant_violated(
1269 source_path: &str,
1270 field_name: &str,
1271 target_path: &str,
1272 ) -> Self {
1273 Self::executor_internal(format!(
1274 "relation target decode invariant violated while preparing reverse index: source={source_path} field={field_name} target={target_path}",
1275 ))
1276 }
1277
1278 pub(crate) fn bytes_covering_component_payload_empty() -> Self {
1280 Self::index_corruption("index component payload is empty during covering projection decode")
1281 }
1282
1283 pub(crate) fn bytes_covering_bool_payload_truncated() -> Self {
1285 Self::index_corruption("bool covering component payload is truncated")
1286 }
1287
1288 pub(crate) fn bytes_covering_component_payload_invalid_length(payload_kind: &str) -> Self {
1290 Self::index_corruption(format!(
1291 "{payload_kind} covering component payload has invalid length"
1292 ))
1293 }
1294
1295 pub(crate) fn bytes_covering_bool_payload_invalid_value() -> Self {
1297 Self::index_corruption("bool covering component payload has invalid value")
1298 }
1299
1300 pub(crate) fn bytes_covering_text_payload_invalid_terminator() -> Self {
1302 Self::index_corruption("text covering component payload has invalid terminator")
1303 }
1304
1305 pub(crate) fn bytes_covering_text_payload_trailing_bytes() -> Self {
1307 Self::index_corruption("text covering component payload contains trailing bytes")
1308 }
1309
1310 pub(crate) fn bytes_covering_text_payload_invalid_utf8() -> Self {
1312 Self::index_corruption("text covering component payload is not valid UTF-8")
1313 }
1314
1315 pub(crate) fn bytes_covering_text_payload_invalid_escape_byte() -> Self {
1317 Self::index_corruption("text covering component payload has invalid escape byte")
1318 }
1319
1320 pub(crate) fn bytes_covering_text_payload_missing_terminator() -> Self {
1322 Self::index_corruption("text covering component payload is missing terminator")
1323 }
1324
1325 #[must_use]
1327 pub fn missing_persisted_slot(field_name: &'static str) -> Self {
1328 Self::serialize_corruption(format!("row decode: missing required field '{field_name}'"))
1329 }
1330
1331 pub(crate) fn identity_corruption(message: impl Into<String>) -> Self {
1333 Self::new(
1334 ErrorClass::Corruption,
1335 ErrorOrigin::Identity,
1336 message.into(),
1337 )
1338 }
1339
1340 #[cold]
1342 #[inline(never)]
1343 pub(crate) fn store_unsupported(message: impl Into<String>) -> Self {
1344 Self::new(ErrorClass::Unsupported, ErrorOrigin::Store, message.into())
1345 }
1346
1347 pub(crate) fn unsupported_entity_tag_in_data_store(
1349 entity_tag: crate::types::EntityTag,
1350 ) -> Self {
1351 Self::store_unsupported(format!(
1352 "unsupported entity tag in data store: '{}'",
1353 entity_tag.value()
1354 ))
1355 }
1356
1357 #[cfg_attr(test, allow(dead_code))]
1359 pub(crate) fn commit_memory_id_registration_failed(err: impl fmt::Display) -> Self {
1360 Self::store_internal(format!("commit memory id registration failed: {err}"))
1361 }
1362
1363 pub(crate) fn index_unsupported(message: impl Into<String>) -> Self {
1365 Self::new(ErrorClass::Unsupported, ErrorOrigin::Index, message.into())
1366 }
1367
1368 pub(crate) fn index_component_exceeds_max_size(
1370 key_item: impl fmt::Display,
1371 len: usize,
1372 max_component_size: usize,
1373 ) -> Self {
1374 Self::index_unsupported(format!(
1375 "index component exceeds max size: key item '{key_item}' -> {len} bytes (limit {max_component_size})",
1376 ))
1377 }
1378
1379 pub(crate) fn serialize_unsupported(message: impl Into<String>) -> Self {
1381 Self::new(
1382 ErrorClass::Unsupported,
1383 ErrorOrigin::Serialize,
1384 message.into(),
1385 )
1386 }
1387
1388 pub(crate) fn cursor_unsupported(message: impl Into<String>) -> Self {
1390 Self::new(ErrorClass::Unsupported, ErrorOrigin::Cursor, message.into())
1391 }
1392
1393 pub(crate) fn serialize_incompatible_persisted_format(message: impl Into<String>) -> Self {
1395 Self::new(
1396 ErrorClass::IncompatiblePersistedFormat,
1397 ErrorOrigin::Serialize,
1398 message.into(),
1399 )
1400 }
1401
1402 #[cfg(feature = "sql")]
1405 pub(crate) fn query_unsupported_sql_feature(feature: &'static str) -> Self {
1406 let message = format!(
1407 "SQL query is not executable in this release: unsupported SQL feature: {feature}"
1408 );
1409
1410 Self {
1411 class: ErrorClass::Unsupported,
1412 origin: ErrorOrigin::Query,
1413 message,
1414 detail: Some(ErrorDetail::Query(
1415 QueryErrorDetail::UnsupportedSqlFeature { feature },
1416 )),
1417 }
1418 }
1419
1420 pub fn store_not_found(key: impl Into<String>) -> Self {
1421 let key = key.into();
1422
1423 Self {
1424 class: ErrorClass::NotFound,
1425 origin: ErrorOrigin::Store,
1426 message: format!("data key not found: {key}"),
1427 detail: Some(ErrorDetail::Store(StoreError::NotFound { key })),
1428 }
1429 }
1430
1431 pub fn unsupported_entity_path(path: impl Into<String>) -> Self {
1433 let path = path.into();
1434
1435 Self::new(
1436 ErrorClass::Unsupported,
1437 ErrorOrigin::Store,
1438 format!("unsupported entity path: '{path}'"),
1439 )
1440 }
1441
1442 #[must_use]
1443 pub const fn is_not_found(&self) -> bool {
1444 matches!(
1445 self.detail,
1446 Some(ErrorDetail::Store(StoreError::NotFound { .. }))
1447 )
1448 }
1449
1450 #[must_use]
1451 pub fn display_with_class(&self) -> String {
1452 format!("{}:{}: {}", self.origin, self.class, self.message)
1453 }
1454
1455 #[cold]
1457 #[inline(never)]
1458 pub(crate) fn index_plan_corruption(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1459 let message = message.into();
1460 Self::new(
1461 ErrorClass::Corruption,
1462 origin,
1463 format!("corruption detected ({origin}): {message}"),
1464 )
1465 }
1466
1467 #[cold]
1469 #[inline(never)]
1470 pub(crate) fn index_plan_index_corruption(message: impl Into<String>) -> Self {
1471 Self::index_plan_corruption(ErrorOrigin::Index, message)
1472 }
1473
1474 #[cold]
1476 #[inline(never)]
1477 pub(crate) fn index_plan_store_corruption(message: impl Into<String>) -> Self {
1478 Self::index_plan_corruption(ErrorOrigin::Store, message)
1479 }
1480
1481 #[cold]
1483 #[inline(never)]
1484 pub(crate) fn index_plan_serialize_corruption(message: impl Into<String>) -> Self {
1485 Self::index_plan_corruption(ErrorOrigin::Serialize, message)
1486 }
1487
1488 #[cfg(test)]
1490 pub(crate) fn index_plan_invariant(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1491 let message = message.into();
1492 Self::new(
1493 ErrorClass::InvariantViolation,
1494 origin,
1495 format!("invariant violation detected ({origin}): {message}"),
1496 )
1497 }
1498
1499 #[cfg(test)]
1501 pub(crate) fn index_plan_store_invariant(message: impl Into<String>) -> Self {
1502 Self::index_plan_invariant(ErrorOrigin::Store, message)
1503 }
1504
1505 pub(crate) fn index_violation(path: &str, index_fields: &[&str]) -> Self {
1507 Self::new(
1508 ErrorClass::Conflict,
1509 ErrorOrigin::Index,
1510 format!(
1511 "index constraint violation: {path} ({})",
1512 index_fields.join(", ")
1513 ),
1514 )
1515 }
1516}
1517
1518#[derive(Debug, ThisError)]
1526pub enum ErrorDetail {
1527 #[error("{0}")]
1528 Store(StoreError),
1529 #[error("{0}")]
1530 Query(QueryErrorDetail),
1531 }
1538
1539#[derive(Debug, ThisError)]
1547pub enum StoreError {
1548 #[error("key not found: {key}")]
1549 NotFound { key: String },
1550
1551 #[error("store corruption: {message}")]
1552 Corrupt { message: String },
1553
1554 #[error("store invariant violation: {message}")]
1555 InvariantViolation { message: String },
1556}
1557
1558#[derive(Debug, ThisError)]
1565pub enum QueryErrorDetail {
1566 #[error("numeric overflow")]
1567 NumericOverflow,
1568
1569 #[error("numeric result is not representable")]
1570 NumericNotRepresentable,
1571
1572 #[error("unsupported SQL feature: {feature}")]
1573 UnsupportedSqlFeature { feature: &'static str },
1574}
1575
1576#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1583pub enum ErrorClass {
1584 Corruption,
1585 IncompatiblePersistedFormat,
1586 NotFound,
1587 Internal,
1588 Conflict,
1589 Unsupported,
1590 InvariantViolation,
1591}
1592
1593impl fmt::Display for ErrorClass {
1594 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1595 let label = match self {
1596 Self::Corruption => "corruption",
1597 Self::IncompatiblePersistedFormat => "incompatible_persisted_format",
1598 Self::NotFound => "not_found",
1599 Self::Internal => "internal",
1600 Self::Conflict => "conflict",
1601 Self::Unsupported => "unsupported",
1602 Self::InvariantViolation => "invariant_violation",
1603 };
1604 write!(f, "{label}")
1605 }
1606}
1607
1608#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1615pub enum ErrorOrigin {
1616 Serialize,
1617 Store,
1618 Index,
1619 Identity,
1620 Query,
1621 Planner,
1622 Cursor,
1623 Recovery,
1624 Response,
1625 Executor,
1626 Interface,
1627}
1628
1629impl fmt::Display for ErrorOrigin {
1630 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1631 let label = match self {
1632 Self::Serialize => "serialize",
1633 Self::Store => "store",
1634 Self::Index => "index",
1635 Self::Identity => "identity",
1636 Self::Query => "query",
1637 Self::Planner => "planner",
1638 Self::Cursor => "cursor",
1639 Self::Recovery => "recovery",
1640 Self::Response => "response",
1641 Self::Executor => "executor",
1642 Self::Interface => "interface",
1643 };
1644 write!(f, "{label}")
1645 }
1646}