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_entity_field_type_mismatch(
383 entity_path: &str,
384 field_name: &str,
385 value: &crate::value::Value,
386 ) -> Self {
387 Self::executor_invariant(format!(
388 "entity field type mismatch: {entity_path} field={field_name} value={value:?}",
389 ))
390 }
391
392 pub(crate) fn mutation_generated_field_explicit(entity_path: &str, field_name: &str) -> Self {
394 Self::executor_unsupported(format!(
395 "generated field may not be explicitly written: {entity_path} field={field_name}",
396 ))
397 }
398
399 pub(crate) fn mutation_create_missing_authored_fields(
401 entity_path: &str,
402 field_names: &str,
403 ) -> Self {
404 Self::executor_unsupported(format!(
405 "create requires explicit values for authorable fields {field_names}: {entity_path}",
406 ))
407 }
408
409 pub(crate) fn mutation_structural_after_image_invalid(
414 entity_path: &str,
415 data_key: impl fmt::Display,
416 detail: impl AsRef<str>,
417 ) -> Self {
418 Self::executor_invariant(format!(
419 "mutation result is invalid: {entity_path} key={data_key} ({})",
420 detail.as_ref(),
421 ))
422 }
423
424 pub(crate) fn mutation_structural_field_unknown(entity_path: &str, field_name: &str) -> Self {
426 Self::executor_invariant(format!(
427 "mutation field not found: {entity_path} field={field_name}",
428 ))
429 }
430
431 pub(crate) fn mutation_decimal_scale_mismatch(
433 entity_path: &str,
434 field_name: &str,
435 expected_scale: impl fmt::Display,
436 actual_scale: impl fmt::Display,
437 ) -> Self {
438 Self::executor_unsupported(format!(
439 "decimal field scale mismatch: {entity_path} field={field_name} expected_scale={expected_scale} actual_scale={actual_scale}",
440 ))
441 }
442
443 pub(crate) fn mutation_text_max_len_exceeded(
445 entity_path: &str,
446 field_name: &str,
447 max_len: impl fmt::Display,
448 actual_len: impl fmt::Display,
449 ) -> Self {
450 Self::executor_unsupported(format!(
451 "text length exceeds max_len: {entity_path} field={field_name} max_len={max_len} actual_len={actual_len}",
452 ))
453 }
454
455 pub(crate) fn mutation_set_field_list_required(entity_path: &str, field_name: &str) -> Self {
457 Self::executor_invariant(format!(
458 "set field must encode as Value::List: {entity_path} field={field_name}",
459 ))
460 }
461
462 pub(crate) fn mutation_set_field_not_canonical(entity_path: &str, field_name: &str) -> Self {
464 Self::executor_invariant(format!(
465 "set field must be strictly ordered and deduplicated: {entity_path} field={field_name}",
466 ))
467 }
468
469 pub(crate) fn mutation_map_field_map_required(entity_path: &str, field_name: &str) -> Self {
471 Self::executor_invariant(format!(
472 "map field must encode as Value::Map: {entity_path} field={field_name}",
473 ))
474 }
475
476 pub(crate) fn mutation_map_field_entries_invalid(
478 entity_path: &str,
479 field_name: &str,
480 detail: impl fmt::Display,
481 ) -> Self {
482 Self::executor_invariant(format!(
483 "map field entries violate map invariants: {entity_path} field={field_name} ({detail})",
484 ))
485 }
486
487 pub(crate) fn mutation_map_field_entries_not_canonical(
489 entity_path: &str,
490 field_name: &str,
491 ) -> Self {
492 Self::executor_invariant(format!(
493 "map field entries are not in canonical deterministic order: {entity_path} field={field_name}",
494 ))
495 }
496
497 pub(crate) fn scalar_page_ordering_after_filtering_required() -> Self {
499 Self::query_executor_invariant("ordering must run after filtering")
500 }
501
502 pub(crate) fn scalar_page_cursor_boundary_order_required() -> Self {
504 Self::query_executor_invariant("cursor boundary requires ordering")
505 }
506
507 pub(crate) fn scalar_page_cursor_boundary_after_ordering_required() -> Self {
509 Self::query_executor_invariant("cursor boundary must run after ordering")
510 }
511
512 pub(crate) fn scalar_page_pagination_after_ordering_required() -> Self {
514 Self::query_executor_invariant("pagination must run after ordering")
515 }
516
517 pub(crate) fn scalar_page_delete_limit_after_ordering_required() -> Self {
519 Self::query_executor_invariant("delete limit must run after ordering")
520 }
521
522 pub(crate) fn load_runtime_scalar_payload_required() -> Self {
524 Self::query_executor_invariant("scalar load mode must carry scalar runtime payload")
525 }
526
527 pub(crate) fn load_runtime_grouped_payload_required() -> Self {
529 Self::query_executor_invariant("grouped load mode must carry grouped runtime payload")
530 }
531
532 pub(crate) fn load_runtime_scalar_surface_payload_required() -> Self {
534 Self::query_executor_invariant("scalar page load mode must carry scalar runtime payload")
535 }
536
537 pub(crate) fn load_runtime_grouped_surface_payload_required() -> Self {
539 Self::query_executor_invariant("grouped page load mode must carry grouped runtime payload")
540 }
541
542 pub(crate) fn load_executor_load_plan_required() -> Self {
544 Self::query_executor_invariant("load executor requires load plans")
545 }
546
547 pub(crate) fn delete_executor_grouped_unsupported() -> Self {
549 Self::executor_unsupported("grouped query execution is not yet enabled in this release")
550 }
551
552 pub(crate) fn delete_executor_delete_plan_required() -> Self {
554 Self::query_executor_invariant("delete executor requires delete plans")
555 }
556
557 pub(crate) fn aggregate_fold_mode_terminal_contract_required() -> Self {
559 Self::query_executor_invariant(
560 "aggregate fold mode must match route fold-mode contract for aggregate terminal",
561 )
562 }
563
564 pub(crate) fn fast_stream_route_kind_request_match_required() -> Self {
566 Self::query_executor_invariant("fast-stream route kind/request mismatch")
567 }
568
569 pub(crate) fn secondary_index_prefix_spec_required() -> Self {
571 Self::query_executor_invariant(
572 "index-prefix executable spec must be materialized for index-prefix plans",
573 )
574 }
575
576 pub(crate) fn index_range_limit_spec_required() -> Self {
578 Self::query_executor_invariant(
579 "index-range executable spec must be materialized for index-range plans",
580 )
581 }
582
583 pub(crate) fn mutation_atomic_save_duplicate_key(
585 entity_path: &str,
586 key: impl fmt::Display,
587 ) -> Self {
588 Self::executor_unsupported(format!(
589 "atomic save batch rejected duplicate key: entity={entity_path} key={key}",
590 ))
591 }
592
593 pub(crate) fn mutation_index_store_generation_changed(
595 expected_generation: u64,
596 observed_generation: u64,
597 ) -> Self {
598 Self::executor_invariant(format!(
599 "index store generation changed between preflight and apply: expected {expected_generation}, found {observed_generation}",
600 ))
601 }
602
603 #[must_use]
605 #[cold]
606 #[inline(never)]
607 pub(crate) fn executor_invariant_message(reason: impl Into<String>) -> String {
608 format!("executor invariant violated: {}", reason.into())
609 }
610
611 #[cold]
613 #[inline(never)]
614 pub(crate) fn planner_invariant(message: impl Into<String>) -> Self {
615 Self::new(
616 ErrorClass::InvariantViolation,
617 ErrorOrigin::Planner,
618 message.into(),
619 )
620 }
621
622 #[must_use]
624 pub(crate) fn invalid_logical_plan_message(reason: impl Into<String>) -> String {
625 format!("invalid logical plan: {}", reason.into())
626 }
627
628 pub(crate) fn query_invalid_logical_plan(reason: impl Into<String>) -> Self {
630 Self::planner_invariant(Self::invalid_logical_plan_message(reason))
631 }
632
633 #[cold]
635 #[inline(never)]
636 pub(crate) fn query_invariant(message: impl Into<String>) -> Self {
637 Self::new(
638 ErrorClass::InvariantViolation,
639 ErrorOrigin::Query,
640 message.into(),
641 )
642 }
643
644 pub(crate) fn store_invariant(message: impl Into<String>) -> Self {
646 Self::new(
647 ErrorClass::InvariantViolation,
648 ErrorOrigin::Store,
649 message.into(),
650 )
651 }
652
653 pub(crate) fn duplicate_runtime_hooks_for_entity_tag(
655 entity_tag: crate::types::EntityTag,
656 ) -> Self {
657 Self::store_invariant(format!(
658 "duplicate runtime hooks for entity tag '{}'",
659 entity_tag.value()
660 ))
661 }
662
663 pub(crate) fn duplicate_runtime_hooks_for_entity_path(entity_path: &str) -> Self {
665 Self::store_invariant(format!(
666 "duplicate runtime hooks for entity path '{entity_path}'"
667 ))
668 }
669
670 #[cold]
672 #[inline(never)]
673 pub(crate) fn store_internal(message: impl Into<String>) -> Self {
674 Self::new(ErrorClass::Internal, ErrorOrigin::Store, message.into())
675 }
676
677 pub(crate) fn commit_memory_id_unconfigured() -> Self {
679 Self::store_internal(
680 "commit memory id is not configured; initialize recovery before commit store access",
681 )
682 }
683
684 pub(crate) fn commit_memory_id_mismatch(cached_id: u8, configured_id: u8) -> Self {
686 Self::store_internal(format!(
687 "commit memory id mismatch: cached={cached_id}, configured={configured_id}",
688 ))
689 }
690
691 pub(crate) fn delete_rollback_row_required() -> Self {
693 Self::store_internal("missing raw row for delete rollback")
694 }
695
696 pub(crate) fn commit_memory_registry_init_failed(err: impl fmt::Display) -> Self {
698 Self::store_internal(format!("memory registry init failed: {err}"))
699 }
700
701 pub(crate) fn recovery_integrity_validation_failed(
703 missing_index_entries: u64,
704 divergent_index_entries: u64,
705 orphan_index_references: u64,
706 ) -> Self {
707 Self::store_corruption(format!(
708 "recovery integrity validation failed: missing_index_entries={missing_index_entries} divergent_index_entries={divergent_index_entries} orphan_index_references={orphan_index_references}",
709 ))
710 }
711
712 #[cold]
714 #[inline(never)]
715 pub(crate) fn index_internal(message: impl Into<String>) -> Self {
716 Self::new(ErrorClass::Internal, ErrorOrigin::Index, message.into())
717 }
718
719 pub(crate) fn structural_index_removal_entity_key_required() -> Self {
721 Self::index_internal("missing old entity key for structural index removal")
722 }
723
724 pub(crate) fn structural_index_insertion_entity_key_required() -> Self {
726 Self::index_internal("missing new entity key for structural index insertion")
727 }
728
729 pub(crate) fn index_commit_op_old_entity_key_required() -> Self {
731 Self::index_internal("missing old entity key for index removal")
732 }
733
734 pub(crate) fn index_commit_op_new_entity_key_required() -> Self {
736 Self::index_internal("missing new entity key for index insertion")
737 }
738
739 #[cfg(test)]
741 pub(crate) fn query_internal(message: impl Into<String>) -> Self {
742 Self::new(ErrorClass::Internal, ErrorOrigin::Query, message.into())
743 }
744
745 #[cold]
747 #[inline(never)]
748 pub(crate) fn query_unsupported(message: impl Into<String>) -> Self {
749 Self::new(ErrorClass::Unsupported, ErrorOrigin::Query, message.into())
750 }
751
752 #[cold]
754 #[inline(never)]
755 pub(crate) fn query_numeric_overflow() -> Self {
756 Self {
757 class: ErrorClass::Unsupported,
758 origin: ErrorOrigin::Query,
759 message: "numeric overflow".to_string(),
760 detail: Some(ErrorDetail::Query(QueryErrorDetail::NumericOverflow)),
761 }
762 }
763
764 #[cold]
767 #[inline(never)]
768 pub(crate) fn query_numeric_not_representable() -> Self {
769 Self {
770 class: ErrorClass::Unsupported,
771 origin: ErrorOrigin::Query,
772 message: "numeric result is not representable".to_string(),
773 detail: Some(ErrorDetail::Query(
774 QueryErrorDetail::NumericNotRepresentable,
775 )),
776 }
777 }
778
779 #[cold]
781 #[inline(never)]
782 pub(crate) fn serialize_internal(message: impl Into<String>) -> Self {
783 Self::new(ErrorClass::Internal, ErrorOrigin::Serialize, message.into())
784 }
785
786 pub(crate) fn persisted_row_encode_failed(detail: impl fmt::Display) -> Self {
788 Self::serialize_internal(format!("row encode failed: {detail}"))
789 }
790
791 pub(crate) fn persisted_row_field_encode_failed(
793 field_name: &str,
794 detail: impl fmt::Display,
795 ) -> Self {
796 Self::serialize_internal(format!(
797 "row encode failed for field '{field_name}': {detail}",
798 ))
799 }
800
801 pub(crate) fn bytes_field_value_encode_failed(detail: impl fmt::Display) -> Self {
803 Self::serialize_internal(format!("bytes(field) value encode failed: {detail}"))
804 }
805
806 #[cold]
808 #[inline(never)]
809 pub(crate) fn store_corruption(message: impl Into<String>) -> Self {
810 Self::new(ErrorClass::Corruption, ErrorOrigin::Store, message.into())
811 }
812
813 pub(crate) fn multiple_commit_memory_ids_registered(ids: impl fmt::Debug) -> Self {
815 Self::store_corruption(format!(
816 "multiple commit marker memory ids registered: {ids:?}"
817 ))
818 }
819
820 pub(crate) fn commit_corruption(detail: impl fmt::Display) -> Self {
822 Self::store_corruption(format!("commit marker corrupted: {detail}"))
823 }
824
825 pub(crate) fn commit_component_corruption(component: &str, detail: impl fmt::Display) -> Self {
827 Self::store_corruption(format!("commit marker {component} corrupted: {detail}"))
828 }
829
830 pub(crate) fn commit_id_generation_failed(detail: impl fmt::Display) -> Self {
832 Self::store_internal(format!("commit id generation failed: {detail}"))
833 }
834
835 pub(crate) fn commit_marker_payload_exceeds_u32_length_limit(label: &str, len: usize) -> Self {
837 Self::store_unsupported(format!("{label} exceeds u32 length limit: {len} bytes"))
838 }
839
840 pub(crate) fn commit_component_length_invalid(
842 component: &str,
843 len: usize,
844 expected: impl fmt::Display,
845 ) -> Self {
846 Self::commit_component_corruption(
847 component,
848 format!("invalid length {len}, expected {expected}"),
849 )
850 }
851
852 pub(crate) fn commit_marker_exceeds_max_size(size: usize, max_size: u32) -> Self {
854 Self::commit_corruption(format!(
855 "commit marker exceeds max size: {size} bytes (limit {max_size})",
856 ))
857 }
858
859 #[cfg(test)]
861 pub(crate) fn commit_marker_exceeds_max_size_before_persist(
862 size: usize,
863 max_size: u32,
864 ) -> Self {
865 Self::store_unsupported(format!(
866 "commit marker exceeds max size: {size} bytes (limit {max_size})",
867 ))
868 }
869
870 pub(crate) fn commit_control_slot_exceeds_max_size(size: usize, max_size: u32) -> Self {
872 Self::store_unsupported(format!(
873 "commit control slot exceeds max size: {size} bytes (limit {max_size})",
874 ))
875 }
876
877 pub(crate) fn commit_control_slot_marker_bytes_exceed_u32_length_limit(size: usize) -> Self {
879 Self::store_unsupported(format!(
880 "commit marker bytes exceed u32 length limit: {size} bytes",
881 ))
882 }
883
884 pub(crate) fn startup_index_rebuild_invalid_data_key(
886 store_path: &str,
887 detail: impl fmt::Display,
888 ) -> Self {
889 Self::store_corruption(format!(
890 "startup index rebuild failed: invalid data key in store '{store_path}' ({detail})",
891 ))
892 }
893
894 #[cold]
896 #[inline(never)]
897 pub(crate) fn index_corruption(message: impl Into<String>) -> Self {
898 Self::new(ErrorClass::Corruption, ErrorOrigin::Index, message.into())
899 }
900
901 pub(crate) fn index_unique_validation_corruption(
903 entity_path: &str,
904 fields: &str,
905 detail: impl fmt::Display,
906 ) -> Self {
907 Self::index_plan_index_corruption(format!(
908 "index corrupted: {entity_path} ({fields}) -> {detail}",
909 ))
910 }
911
912 pub(crate) fn structural_index_entry_corruption(
914 entity_path: &str,
915 fields: &str,
916 detail: impl fmt::Display,
917 ) -> Self {
918 Self::index_plan_index_corruption(format!(
919 "index corrupted: {entity_path} ({fields}) -> {detail}",
920 ))
921 }
922
923 pub(crate) fn index_unique_validation_entity_key_required() -> Self {
925 Self::index_invariant("missing entity key during unique validation")
926 }
927
928 pub(crate) fn index_unique_validation_row_deserialize_failed(
930 data_key: impl fmt::Display,
931 source: impl fmt::Display,
932 ) -> Self {
933 Self::index_plan_serialize_corruption(format!(
934 "failed to structurally deserialize row: {data_key} ({source})"
935 ))
936 }
937
938 pub(crate) fn index_unique_validation_primary_key_decode_failed(
940 data_key: impl fmt::Display,
941 source: impl fmt::Display,
942 ) -> Self {
943 Self::index_plan_serialize_corruption(format!(
944 "failed to decode structural primary-key slot: {data_key} ({source})"
945 ))
946 }
947
948 pub(crate) fn index_unique_validation_key_rebuild_failed(
950 data_key: impl fmt::Display,
951 entity_path: &str,
952 source: impl fmt::Display,
953 ) -> Self {
954 Self::index_plan_serialize_corruption(format!(
955 "failed to structurally decode unique key row {data_key} for {entity_path}: {source}",
956 ))
957 }
958
959 pub(crate) fn index_unique_validation_row_required(data_key: impl fmt::Display) -> Self {
961 Self::index_plan_store_corruption(format!("missing row: {data_key}"))
962 }
963
964 pub(crate) fn index_only_predicate_component_required() -> Self {
966 Self::index_invariant("index-only predicate program referenced missing index component")
967 }
968
969 pub(crate) fn index_scan_continuation_anchor_within_envelope_required() -> Self {
971 Self::index_invariant(
972 "index-range continuation anchor is outside the requested range envelope",
973 )
974 }
975
976 pub(crate) fn index_scan_continuation_advancement_required() -> Self {
978 Self::index_invariant("index-range continuation scan did not advance beyond the anchor")
979 }
980
981 pub(crate) fn index_scan_key_corrupted_during(
983 context: &'static str,
984 err: impl fmt::Display,
985 ) -> Self {
986 Self::index_corruption(format!("index key corrupted during {context}: {err}"))
987 }
988
989 pub(crate) fn index_projection_component_required(
991 index_name: &str,
992 component_index: usize,
993 ) -> Self {
994 Self::index_invariant(format!(
995 "index projection referenced missing component: index='{index_name}' component_index={component_index}",
996 ))
997 }
998
999 pub(crate) fn unique_index_entry_single_key_required() -> Self {
1001 Self::index_corruption("unique index entry contains an unexpected number of keys")
1002 }
1003
1004 pub(crate) fn index_entry_decode_failed(err: impl fmt::Display) -> Self {
1006 Self::index_corruption(err.to_string())
1007 }
1008
1009 pub(crate) fn serialize_corruption(message: impl Into<String>) -> Self {
1011 Self::new(
1012 ErrorClass::Corruption,
1013 ErrorOrigin::Serialize,
1014 message.into(),
1015 )
1016 }
1017
1018 pub(crate) fn persisted_row_decode_failed(detail: impl fmt::Display) -> Self {
1020 Self::serialize_corruption(format!("row decode: {detail}"))
1021 }
1022
1023 pub(crate) fn persisted_row_field_decode_failed(
1025 field_name: &str,
1026 detail: impl fmt::Display,
1027 ) -> Self {
1028 Self::serialize_corruption(format!(
1029 "row decode failed for field '{field_name}': {detail}",
1030 ))
1031 }
1032
1033 pub(crate) fn persisted_row_field_kind_decode_failed(
1035 field_name: &str,
1036 field_kind: impl fmt::Debug,
1037 detail: impl fmt::Display,
1038 ) -> Self {
1039 Self::persisted_row_field_decode_failed(
1040 field_name,
1041 format!("kind={field_kind:?}: {detail}"),
1042 )
1043 }
1044
1045 pub(crate) fn persisted_row_field_payload_exact_len_required(
1047 field_name: &str,
1048 payload_kind: &str,
1049 expected_len: usize,
1050 ) -> Self {
1051 let unit = if expected_len == 1 { "byte" } else { "bytes" };
1052
1053 Self::persisted_row_field_decode_failed(
1054 field_name,
1055 format!("{payload_kind} payload must be exactly {expected_len} {unit}"),
1056 )
1057 }
1058
1059 pub(crate) fn persisted_row_field_payload_must_be_empty(
1061 field_name: &str,
1062 payload_kind: &str,
1063 ) -> Self {
1064 Self::persisted_row_field_decode_failed(
1065 field_name,
1066 format!("{payload_kind} payload must be empty"),
1067 )
1068 }
1069
1070 pub(crate) fn persisted_row_field_payload_invalid_byte(
1072 field_name: &str,
1073 payload_kind: &str,
1074 value: u8,
1075 ) -> Self {
1076 Self::persisted_row_field_decode_failed(
1077 field_name,
1078 format!("invalid {payload_kind} payload byte {value}"),
1079 )
1080 }
1081
1082 pub(crate) fn persisted_row_field_payload_non_finite(
1084 field_name: &str,
1085 payload_kind: &str,
1086 ) -> Self {
1087 Self::persisted_row_field_decode_failed(
1088 field_name,
1089 format!("{payload_kind} payload is non-finite"),
1090 )
1091 }
1092
1093 pub(crate) fn persisted_row_field_payload_out_of_range(
1095 field_name: &str,
1096 payload_kind: &str,
1097 ) -> Self {
1098 Self::persisted_row_field_decode_failed(
1099 field_name,
1100 format!("{payload_kind} payload out of range for target type"),
1101 )
1102 }
1103
1104 pub(crate) fn persisted_row_field_text_payload_invalid_utf8(
1106 field_name: &str,
1107 detail: impl fmt::Display,
1108 ) -> Self {
1109 Self::persisted_row_field_decode_failed(
1110 field_name,
1111 format!("invalid UTF-8 text payload ({detail})"),
1112 )
1113 }
1114
1115 pub(crate) fn persisted_row_slot_lookup_out_of_bounds(model_path: &str, slot: usize) -> Self {
1117 Self::index_invariant(format!(
1118 "slot lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1119 ))
1120 }
1121
1122 pub(crate) fn persisted_row_slot_cache_lookup_out_of_bounds(
1124 model_path: &str,
1125 slot: usize,
1126 ) -> Self {
1127 Self::index_invariant(format!(
1128 "slot cache lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1129 ))
1130 }
1131
1132 pub(crate) fn persisted_row_primary_key_not_storage_encodable(
1134 data_key: impl fmt::Debug,
1135 detail: impl fmt::Display,
1136 ) -> Self {
1137 Self::persisted_row_decode_failed(format!(
1138 "primary-key value is not storage-key encodable: {data_key:?} ({detail})",
1139 ))
1140 }
1141
1142 pub(crate) fn persisted_row_primary_key_slot_missing(data_key: impl fmt::Debug) -> Self {
1144 Self::persisted_row_decode_failed(format!(
1145 "missing primary-key slot while validating {data_key:?}",
1146 ))
1147 }
1148
1149 pub(crate) fn persisted_row_key_mismatch(
1151 expected_key: impl fmt::Debug,
1152 found_key: impl fmt::Debug,
1153 ) -> Self {
1154 Self::store_corruption(format!(
1155 "row key mismatch: expected {expected_key:?}, found {found_key:?}",
1156 ))
1157 }
1158
1159 pub(crate) fn persisted_row_declared_field_missing(field_name: &str) -> Self {
1161 Self::persisted_row_decode_failed(format!("missing declared field `{field_name}`"))
1162 }
1163
1164 pub(crate) fn data_key_entity_mismatch(
1166 expected: impl fmt::Display,
1167 found: impl fmt::Display,
1168 ) -> Self {
1169 Self::store_corruption(format!(
1170 "data key entity mismatch: expected {expected}, found {found}",
1171 ))
1172 }
1173
1174 pub(crate) fn reverse_index_ordinal_overflow(
1176 source_path: &str,
1177 field_name: &str,
1178 target_path: &str,
1179 detail: impl fmt::Display,
1180 ) -> Self {
1181 Self::index_internal(format!(
1182 "reverse index ordinal overflow: source={source_path} field={field_name} target={target_path} ({detail})",
1183 ))
1184 }
1185
1186 pub(crate) fn reverse_index_entry_corrupted(
1188 source_path: &str,
1189 field_name: &str,
1190 target_path: &str,
1191 index_key: impl fmt::Debug,
1192 detail: impl fmt::Display,
1193 ) -> Self {
1194 Self::index_corruption(format!(
1195 "reverse index entry corrupted: source={source_path} field={field_name} target={target_path} key={index_key:?} ({detail})",
1196 ))
1197 }
1198
1199 pub(crate) fn reverse_index_entry_encode_failed(
1201 source_path: &str,
1202 field_name: &str,
1203 target_path: &str,
1204 detail: impl fmt::Display,
1205 ) -> Self {
1206 Self::index_unsupported(format!(
1207 "reverse index entry encoding failed: source={source_path} field={field_name} target={target_path} ({detail})",
1208 ))
1209 }
1210
1211 pub(crate) fn relation_target_store_missing(
1213 source_path: &str,
1214 field_name: &str,
1215 target_path: &str,
1216 store_path: &str,
1217 detail: impl fmt::Display,
1218 ) -> Self {
1219 Self::executor_internal(format!(
1220 "relation target store missing: source={source_path} field={field_name} target={target_path} store={store_path} ({detail})",
1221 ))
1222 }
1223
1224 pub(crate) fn relation_target_key_decode_failed(
1226 context_label: &str,
1227 source_path: &str,
1228 field_name: &str,
1229 target_path: &str,
1230 detail: impl fmt::Display,
1231 ) -> Self {
1232 Self::identity_corruption(format!(
1233 "{context_label}: source={source_path} field={field_name} target={target_path} ({detail})",
1234 ))
1235 }
1236
1237 pub(crate) fn relation_target_entity_mismatch(
1239 context_label: &str,
1240 source_path: &str,
1241 field_name: &str,
1242 target_path: &str,
1243 target_entity_name: &str,
1244 expected_tag: impl fmt::Display,
1245 actual_tag: impl fmt::Display,
1246 ) -> Self {
1247 Self::store_corruption(format!(
1248 "{context_label}: source={source_path} field={field_name} target={target_path} expected={target_entity_name} (tag={expected_tag}) actual_tag={actual_tag}",
1249 ))
1250 }
1251
1252 pub(crate) fn relation_source_row_decode_failed(
1254 source_path: &str,
1255 field_name: &str,
1256 target_path: &str,
1257 detail: impl fmt::Display,
1258 ) -> Self {
1259 Self::serialize_corruption(format!(
1260 "relation source row decode: source={source_path} field={field_name} target={target_path} ({detail})",
1261 ))
1262 }
1263
1264 pub(crate) fn relation_source_row_unsupported_scalar_relation_key(
1266 source_path: &str,
1267 field_name: &str,
1268 target_path: &str,
1269 ) -> Self {
1270 Self::serialize_corruption(format!(
1271 "relation source row decode: unsupported scalar relation key: source={source_path} field={field_name} target={target_path}",
1272 ))
1273 }
1274
1275 pub(crate) fn relation_source_row_invalid_field_kind(field_kind: impl fmt::Debug) -> Self {
1277 Self::serialize_corruption(format!(
1278 "invalid strong relation field kind during structural decode: {field_kind:?}"
1279 ))
1280 }
1281
1282 pub(crate) fn relation_source_row_unsupported_key_kind(field_kind: impl fmt::Debug) -> Self {
1284 Self::serialize_corruption(format!(
1285 "unsupported strong relation key kind during structural decode: {field_kind:?}"
1286 ))
1287 }
1288
1289 pub(crate) fn reverse_index_relation_target_decode_invariant_violated(
1291 source_path: &str,
1292 field_name: &str,
1293 target_path: &str,
1294 ) -> Self {
1295 Self::executor_internal(format!(
1296 "relation target decode invariant violated while preparing reverse index: source={source_path} field={field_name} target={target_path}",
1297 ))
1298 }
1299
1300 pub(crate) fn bytes_covering_component_payload_empty() -> Self {
1302 Self::index_corruption("index component payload is empty during covering projection decode")
1303 }
1304
1305 pub(crate) fn bytes_covering_bool_payload_truncated() -> Self {
1307 Self::index_corruption("bool covering component payload is truncated")
1308 }
1309
1310 pub(crate) fn bytes_covering_component_payload_invalid_length(payload_kind: &str) -> Self {
1312 Self::index_corruption(format!(
1313 "{payload_kind} covering component payload has invalid length"
1314 ))
1315 }
1316
1317 pub(crate) fn bytes_covering_bool_payload_invalid_value() -> Self {
1319 Self::index_corruption("bool covering component payload has invalid value")
1320 }
1321
1322 pub(crate) fn bytes_covering_text_payload_invalid_terminator() -> Self {
1324 Self::index_corruption("text covering component payload has invalid terminator")
1325 }
1326
1327 pub(crate) fn bytes_covering_text_payload_trailing_bytes() -> Self {
1329 Self::index_corruption("text covering component payload contains trailing bytes")
1330 }
1331
1332 pub(crate) fn bytes_covering_text_payload_invalid_utf8() -> Self {
1334 Self::index_corruption("text covering component payload is not valid UTF-8")
1335 }
1336
1337 pub(crate) fn bytes_covering_text_payload_invalid_escape_byte() -> Self {
1339 Self::index_corruption("text covering component payload has invalid escape byte")
1340 }
1341
1342 pub(crate) fn bytes_covering_text_payload_missing_terminator() -> Self {
1344 Self::index_corruption("text covering component payload is missing terminator")
1345 }
1346
1347 #[must_use]
1349 pub fn missing_persisted_slot(field_name: &'static str) -> Self {
1350 Self::serialize_corruption(format!("row decode: missing required field '{field_name}'"))
1351 }
1352
1353 pub(crate) fn identity_corruption(message: impl Into<String>) -> Self {
1355 Self::new(
1356 ErrorClass::Corruption,
1357 ErrorOrigin::Identity,
1358 message.into(),
1359 )
1360 }
1361
1362 #[cold]
1364 #[inline(never)]
1365 pub(crate) fn store_unsupported(message: impl Into<String>) -> Self {
1366 Self::new(ErrorClass::Unsupported, ErrorOrigin::Store, message.into())
1367 }
1368
1369 pub(crate) fn unsupported_entity_tag_in_data_store(
1371 entity_tag: crate::types::EntityTag,
1372 ) -> Self {
1373 Self::store_unsupported(format!(
1374 "unsupported entity tag in data store: '{}'",
1375 entity_tag.value()
1376 ))
1377 }
1378
1379 pub(crate) fn configured_commit_memory_id_mismatch(
1381 configured_id: u8,
1382 registered_id: u8,
1383 ) -> Self {
1384 Self::store_unsupported(format!(
1385 "configured commit memory id {configured_id} does not match existing commit marker id {registered_id}",
1386 ))
1387 }
1388
1389 pub(crate) fn commit_memory_id_already_registered(memory_id: u8, label: &str) -> Self {
1391 Self::store_unsupported(format!(
1392 "configured commit memory id {memory_id} is already registered as '{label}'",
1393 ))
1394 }
1395
1396 pub(crate) fn commit_memory_id_outside_reserved_ranges(memory_id: u8) -> Self {
1398 Self::store_unsupported(format!(
1399 "configured commit memory id {memory_id} is outside reserved ranges",
1400 ))
1401 }
1402
1403 pub(crate) fn commit_memory_id_registration_failed(err: impl fmt::Display) -> Self {
1405 Self::store_internal(format!("commit memory id registration failed: {err}"))
1406 }
1407
1408 pub(crate) fn index_unsupported(message: impl Into<String>) -> Self {
1410 Self::new(ErrorClass::Unsupported, ErrorOrigin::Index, message.into())
1411 }
1412
1413 pub(crate) fn index_component_exceeds_max_size(
1415 key_item: impl fmt::Display,
1416 len: usize,
1417 max_component_size: usize,
1418 ) -> Self {
1419 Self::index_unsupported(format!(
1420 "index component exceeds max size: key item '{key_item}' -> {len} bytes (limit {max_component_size})",
1421 ))
1422 }
1423
1424 pub(crate) fn index_entry_exceeds_max_keys(
1426 entity_path: &str,
1427 fields: &str,
1428 keys: usize,
1429 ) -> Self {
1430 Self::index_unsupported(format!(
1431 "index entry exceeds max keys: {entity_path} ({fields}) -> {keys} keys",
1432 ))
1433 }
1434
1435 #[cfg(test)]
1437 pub(crate) fn index_entry_duplicate_keys_unexpected(entity_path: &str, fields: &str) -> Self {
1438 Self::index_invariant(format!(
1439 "index entry unexpectedly contains duplicate keys: {entity_path} ({fields})",
1440 ))
1441 }
1442
1443 pub(crate) fn index_entry_key_encoding_failed(
1445 entity_path: &str,
1446 fields: &str,
1447 err: impl fmt::Display,
1448 ) -> Self {
1449 Self::index_unsupported(format!(
1450 "index entry key encoding failed: {entity_path} ({fields}) -> {err}",
1451 ))
1452 }
1453
1454 pub(crate) fn serialize_unsupported(message: impl Into<String>) -> Self {
1456 Self::new(
1457 ErrorClass::Unsupported,
1458 ErrorOrigin::Serialize,
1459 message.into(),
1460 )
1461 }
1462
1463 pub(crate) fn cursor_unsupported(message: impl Into<String>) -> Self {
1465 Self::new(ErrorClass::Unsupported, ErrorOrigin::Cursor, message.into())
1466 }
1467
1468 pub(crate) fn serialize_incompatible_persisted_format(message: impl Into<String>) -> Self {
1470 Self::new(
1471 ErrorClass::IncompatiblePersistedFormat,
1472 ErrorOrigin::Serialize,
1473 message.into(),
1474 )
1475 }
1476
1477 #[cfg(feature = "sql")]
1480 pub(crate) fn query_unsupported_sql_feature(feature: &'static str) -> Self {
1481 let message = format!(
1482 "SQL query is not executable in this release: unsupported SQL feature: {feature}"
1483 );
1484
1485 Self {
1486 class: ErrorClass::Unsupported,
1487 origin: ErrorOrigin::Query,
1488 message,
1489 detail: Some(ErrorDetail::Query(
1490 QueryErrorDetail::UnsupportedSqlFeature { feature },
1491 )),
1492 }
1493 }
1494
1495 pub fn store_not_found(key: impl Into<String>) -> Self {
1496 let key = key.into();
1497
1498 Self {
1499 class: ErrorClass::NotFound,
1500 origin: ErrorOrigin::Store,
1501 message: format!("data key not found: {key}"),
1502 detail: Some(ErrorDetail::Store(StoreError::NotFound { key })),
1503 }
1504 }
1505
1506 pub fn unsupported_entity_path(path: impl Into<String>) -> Self {
1508 let path = path.into();
1509
1510 Self::new(
1511 ErrorClass::Unsupported,
1512 ErrorOrigin::Store,
1513 format!("unsupported entity path: '{path}'"),
1514 )
1515 }
1516
1517 #[must_use]
1518 pub const fn is_not_found(&self) -> bool {
1519 matches!(
1520 self.detail,
1521 Some(ErrorDetail::Store(StoreError::NotFound { .. }))
1522 )
1523 }
1524
1525 #[must_use]
1526 pub fn display_with_class(&self) -> String {
1527 format!("{}:{}: {}", self.origin, self.class, self.message)
1528 }
1529
1530 #[cold]
1532 #[inline(never)]
1533 pub(crate) fn index_plan_corruption(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1534 let message = message.into();
1535 Self::new(
1536 ErrorClass::Corruption,
1537 origin,
1538 format!("corruption detected ({origin}): {message}"),
1539 )
1540 }
1541
1542 #[cold]
1544 #[inline(never)]
1545 pub(crate) fn index_plan_index_corruption(message: impl Into<String>) -> Self {
1546 Self::index_plan_corruption(ErrorOrigin::Index, message)
1547 }
1548
1549 #[cold]
1551 #[inline(never)]
1552 pub(crate) fn index_plan_store_corruption(message: impl Into<String>) -> Self {
1553 Self::index_plan_corruption(ErrorOrigin::Store, message)
1554 }
1555
1556 #[cold]
1558 #[inline(never)]
1559 pub(crate) fn index_plan_serialize_corruption(message: impl Into<String>) -> Self {
1560 Self::index_plan_corruption(ErrorOrigin::Serialize, message)
1561 }
1562
1563 #[cfg(test)]
1565 pub(crate) fn index_plan_invariant(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1566 let message = message.into();
1567 Self::new(
1568 ErrorClass::InvariantViolation,
1569 origin,
1570 format!("invariant violation detected ({origin}): {message}"),
1571 )
1572 }
1573
1574 #[cfg(test)]
1576 pub(crate) fn index_plan_store_invariant(message: impl Into<String>) -> Self {
1577 Self::index_plan_invariant(ErrorOrigin::Store, message)
1578 }
1579
1580 pub(crate) fn index_violation(path: &str, index_fields: &[&str]) -> Self {
1582 Self::new(
1583 ErrorClass::Conflict,
1584 ErrorOrigin::Index,
1585 format!(
1586 "index constraint violation: {path} ({})",
1587 index_fields.join(", ")
1588 ),
1589 )
1590 }
1591}
1592
1593#[derive(Debug, ThisError)]
1601pub enum ErrorDetail {
1602 #[error("{0}")]
1603 Store(StoreError),
1604 #[error("{0}")]
1605 Query(QueryErrorDetail),
1606 }
1613
1614#[derive(Debug, ThisError)]
1622pub enum StoreError {
1623 #[error("key not found: {key}")]
1624 NotFound { key: String },
1625
1626 #[error("store corruption: {message}")]
1627 Corrupt { message: String },
1628
1629 #[error("store invariant violation: {message}")]
1630 InvariantViolation { message: String },
1631}
1632
1633#[derive(Debug, ThisError)]
1640pub enum QueryErrorDetail {
1641 #[error("numeric overflow")]
1642 NumericOverflow,
1643
1644 #[error("numeric result is not representable")]
1645 NumericNotRepresentable,
1646
1647 #[error("unsupported SQL feature: {feature}")]
1648 UnsupportedSqlFeature { feature: &'static str },
1649}
1650
1651#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1658pub enum ErrorClass {
1659 Corruption,
1660 IncompatiblePersistedFormat,
1661 NotFound,
1662 Internal,
1663 Conflict,
1664 Unsupported,
1665 InvariantViolation,
1666}
1667
1668impl fmt::Display for ErrorClass {
1669 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1670 let label = match self {
1671 Self::Corruption => "corruption",
1672 Self::IncompatiblePersistedFormat => "incompatible_persisted_format",
1673 Self::NotFound => "not_found",
1674 Self::Internal => "internal",
1675 Self::Conflict => "conflict",
1676 Self::Unsupported => "unsupported",
1677 Self::InvariantViolation => "invariant_violation",
1678 };
1679 write!(f, "{label}")
1680 }
1681}
1682
1683#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1690pub enum ErrorOrigin {
1691 Serialize,
1692 Store,
1693 Index,
1694 Identity,
1695 Query,
1696 Planner,
1697 Cursor,
1698 Recovery,
1699 Response,
1700 Executor,
1701 Interface,
1702}
1703
1704impl fmt::Display for ErrorOrigin {
1705 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1706 let label = match self {
1707 Self::Serialize => "serialize",
1708 Self::Store => "store",
1709 Self::Index => "index",
1710 Self::Identity => "identity",
1711 Self::Query => "query",
1712 Self::Planner => "planner",
1713 Self::Cursor => "cursor",
1714 Self::Recovery => "recovery",
1715 Self::Response => "response",
1716 Self::Executor => "executor",
1717 Self::Interface => "interface",
1718 };
1719 write!(f, "{label}")
1720 }
1721}