1#[cfg(test)]
8mod tests;
9
10use crate::serialize::{SerializeError, SerializeErrorKind};
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_schema_invalid(
329 entity_path: &str,
330 detail: impl fmt::Display,
331 ) -> Self {
332 Self::executor_invariant(format!("entity schema invalid for {entity_path}: {detail}"))
333 }
334
335 pub(crate) fn mutation_entity_primary_key_missing(entity_path: &str, field_name: &str) -> Self {
337 Self::executor_invariant(format!(
338 "entity primary key field missing: {entity_path} field={field_name}",
339 ))
340 }
341
342 pub(crate) fn mutation_entity_primary_key_invalid_value(
344 entity_path: &str,
345 field_name: &str,
346 value: &crate::value::Value,
347 ) -> Self {
348 Self::executor_invariant(format!(
349 "entity primary key field has invalid value: {entity_path} field={field_name} value={value:?}",
350 ))
351 }
352
353 pub(crate) fn mutation_entity_primary_key_type_mismatch(
355 entity_path: &str,
356 field_name: &str,
357 value: &crate::value::Value,
358 ) -> Self {
359 Self::executor_invariant(format!(
360 "entity primary key field type mismatch: {entity_path} field={field_name} value={value:?}",
361 ))
362 }
363
364 pub(crate) fn mutation_entity_primary_key_mismatch(
366 entity_path: &str,
367 field_name: &str,
368 field_value: &crate::value::Value,
369 identity_key: &crate::value::Value,
370 ) -> Self {
371 Self::executor_invariant(format!(
372 "entity primary key mismatch: {entity_path} field={field_name} field_value={field_value:?} id_key={identity_key:?}",
373 ))
374 }
375
376 pub(crate) fn mutation_entity_field_missing(
378 entity_path: &str,
379 field_name: &str,
380 indexed: bool,
381 ) -> Self {
382 let indexed_note = if indexed { " (indexed)" } else { "" };
383
384 Self::executor_invariant(format!(
385 "entity field missing: {entity_path} field={field_name}{indexed_note}",
386 ))
387 }
388
389 pub(crate) fn mutation_entity_field_type_mismatch(
391 entity_path: &str,
392 field_name: &str,
393 value: &crate::value::Value,
394 ) -> Self {
395 Self::executor_invariant(format!(
396 "entity field type mismatch: {entity_path} field={field_name} value={value:?}",
397 ))
398 }
399
400 #[allow(dead_code)]
405 pub(crate) fn mutation_structural_after_image_invalid(
406 entity_path: &str,
407 data_key: impl fmt::Display,
408 detail: impl AsRef<str>,
409 ) -> Self {
410 Self::executor_invariant(format!(
411 "structural mutation produced an invalid entity: {entity_path} key={data_key} ({})",
412 detail.as_ref(),
413 ))
414 }
415
416 pub(crate) fn mutation_structural_field_unknown(entity_path: &str, field_name: &str) -> Self {
418 Self::executor_invariant(format!(
419 "unknown field for structural mutation: {entity_path} field={field_name}",
420 ))
421 }
422
423 pub(crate) fn mutation_decimal_scale_mismatch(
425 entity_path: &str,
426 field_name: &str,
427 expected_scale: impl fmt::Display,
428 actual_scale: impl fmt::Display,
429 ) -> Self {
430 Self::executor_unsupported(format!(
431 "decimal field scale mismatch: {entity_path} field={field_name} expected_scale={expected_scale} actual_scale={actual_scale}",
432 ))
433 }
434
435 pub(crate) fn mutation_set_field_list_required(entity_path: &str, field_name: &str) -> Self {
437 Self::executor_invariant(format!(
438 "set field must encode as Value::List: {entity_path} field={field_name}",
439 ))
440 }
441
442 pub(crate) fn mutation_set_field_not_canonical(entity_path: &str, field_name: &str) -> Self {
444 Self::executor_invariant(format!(
445 "set field must be strictly ordered and deduplicated: {entity_path} field={field_name}",
446 ))
447 }
448
449 pub(crate) fn mutation_map_field_map_required(entity_path: &str, field_name: &str) -> Self {
451 Self::executor_invariant(format!(
452 "map field must encode as Value::Map: {entity_path} field={field_name}",
453 ))
454 }
455
456 pub(crate) fn mutation_map_field_entries_invalid(
458 entity_path: &str,
459 field_name: &str,
460 detail: impl fmt::Display,
461 ) -> Self {
462 Self::executor_invariant(format!(
463 "map field entries violate map invariants: {entity_path} field={field_name} ({detail})",
464 ))
465 }
466
467 pub(crate) fn mutation_map_field_entries_not_canonical(
469 entity_path: &str,
470 field_name: &str,
471 ) -> Self {
472 Self::executor_invariant(format!(
473 "map field entries are not in canonical deterministic order: {entity_path} field={field_name}",
474 ))
475 }
476
477 pub(crate) fn scalar_page_predicate_slots_required() -> Self {
479 Self::query_executor_invariant("post-access filtering requires precompiled predicate slots")
480 }
481
482 pub(crate) fn scalar_page_ordering_after_filtering_required() -> Self {
484 Self::query_executor_invariant("ordering must run after filtering")
485 }
486
487 pub(crate) fn scalar_page_cursor_boundary_order_required() -> Self {
489 Self::query_executor_invariant("cursor boundary requires ordering")
490 }
491
492 pub(crate) fn scalar_page_cursor_boundary_after_ordering_required() -> Self {
494 Self::query_executor_invariant("cursor boundary must run after ordering")
495 }
496
497 pub(crate) fn scalar_page_pagination_after_ordering_required() -> Self {
499 Self::query_executor_invariant("pagination must run after ordering")
500 }
501
502 pub(crate) fn scalar_page_delete_limit_after_ordering_required() -> Self {
504 Self::query_executor_invariant("delete limit must run after ordering")
505 }
506
507 pub(crate) fn load_runtime_scalar_payload_required() -> Self {
509 Self::query_executor_invariant("scalar load mode must carry scalar runtime payload")
510 }
511
512 pub(crate) fn load_runtime_grouped_payload_required() -> Self {
514 Self::query_executor_invariant("grouped load mode must carry grouped runtime payload")
515 }
516
517 pub(crate) fn load_runtime_scalar_surface_payload_required() -> Self {
519 Self::query_executor_invariant("scalar page load mode must carry scalar runtime payload")
520 }
521
522 pub(crate) fn load_runtime_grouped_surface_payload_required() -> Self {
524 Self::query_executor_invariant("grouped page load mode must carry grouped runtime payload")
525 }
526
527 pub(crate) fn load_executor_load_plan_required() -> Self {
529 Self::query_executor_invariant("load executor requires load plans")
530 }
531
532 pub(crate) fn delete_executor_grouped_unsupported() -> Self {
534 Self::executor_unsupported("grouped query execution is not yet enabled in this release")
535 }
536
537 pub(crate) fn delete_executor_delete_plan_required() -> Self {
539 Self::query_executor_invariant("delete executor requires delete plans")
540 }
541
542 pub(crate) fn aggregate_fold_mode_terminal_contract_required() -> Self {
544 Self::query_executor_invariant(
545 "aggregate fold mode must match route fold-mode contract for aggregate terminal",
546 )
547 }
548
549 pub(crate) fn fast_stream_exact_key_count_required() -> Self {
551 Self::query_executor_invariant("fast-path stream must expose an exact key-count hint")
552 }
553
554 pub(crate) fn fast_stream_route_kind_request_match_required() -> Self {
556 Self::query_executor_invariant("fast-stream route kind/request mismatch")
557 }
558
559 pub(crate) fn secondary_index_prefix_spec_required() -> Self {
561 Self::query_executor_invariant(
562 "index-prefix executable spec must be materialized for index-prefix plans",
563 )
564 }
565
566 pub(crate) fn index_range_limit_spec_required() -> Self {
568 Self::query_executor_invariant(
569 "index-range executable spec must be materialized for index-range plans",
570 )
571 }
572
573 pub(crate) fn row_layout_primary_key_slot_required() -> Self {
575 Self::query_executor_invariant("row layout missing primary-key slot")
576 }
577
578 pub(crate) fn mutation_atomic_save_duplicate_key(
580 entity_path: &str,
581 key: impl fmt::Display,
582 ) -> Self {
583 Self::executor_unsupported(format!(
584 "atomic save batch rejected duplicate key: entity={entity_path} key={key}",
585 ))
586 }
587
588 pub(crate) fn mutation_index_store_generation_changed(
590 expected_generation: u64,
591 observed_generation: u64,
592 ) -> Self {
593 Self::executor_invariant(format!(
594 "index store generation changed between preflight and apply: expected {expected_generation}, found {observed_generation}",
595 ))
596 }
597
598 #[must_use]
600 #[cold]
601 #[inline(never)]
602 pub(crate) fn executor_invariant_message(reason: impl Into<String>) -> String {
603 format!("executor invariant violated: {}", reason.into())
604 }
605
606 #[cold]
608 #[inline(never)]
609 pub(crate) fn planner_invariant(message: impl Into<String>) -> Self {
610 Self::new(
611 ErrorClass::InvariantViolation,
612 ErrorOrigin::Planner,
613 message.into(),
614 )
615 }
616
617 #[must_use]
619 pub(crate) fn invalid_logical_plan_message(reason: impl Into<String>) -> String {
620 format!("invalid logical plan: {}", reason.into())
621 }
622
623 pub(crate) fn query_invalid_logical_plan(reason: impl Into<String>) -> Self {
625 Self::planner_invariant(Self::invalid_logical_plan_message(reason))
626 }
627
628 #[cold]
630 #[inline(never)]
631 pub(crate) fn query_invariant(message: impl Into<String>) -> Self {
632 Self::new(
633 ErrorClass::InvariantViolation,
634 ErrorOrigin::Query,
635 message.into(),
636 )
637 }
638
639 pub(crate) fn store_invariant(message: impl Into<String>) -> Self {
641 Self::new(
642 ErrorClass::InvariantViolation,
643 ErrorOrigin::Store,
644 message.into(),
645 )
646 }
647
648 pub(crate) fn duplicate_runtime_hooks_for_entity_tag(
650 entity_tag: crate::types::EntityTag,
651 ) -> Self {
652 Self::store_invariant(format!(
653 "duplicate runtime hooks for entity tag '{}'",
654 entity_tag.value()
655 ))
656 }
657
658 pub(crate) fn duplicate_runtime_hooks_for_entity_path(entity_path: &str) -> Self {
660 Self::store_invariant(format!(
661 "duplicate runtime hooks for entity path '{entity_path}'"
662 ))
663 }
664
665 #[cold]
667 #[inline(never)]
668 pub(crate) fn store_internal(message: impl Into<String>) -> Self {
669 Self::new(ErrorClass::Internal, ErrorOrigin::Store, message.into())
670 }
671
672 pub(crate) fn commit_memory_id_unconfigured() -> Self {
674 Self::store_internal(
675 "commit memory id is not configured; initialize recovery before commit store access",
676 )
677 }
678
679 pub(crate) fn commit_memory_id_mismatch(cached_id: u8, configured_id: u8) -> Self {
681 Self::store_internal(format!(
682 "commit memory id mismatch: cached={cached_id}, configured={configured_id}",
683 ))
684 }
685
686 pub(crate) fn commit_memory_registry_init_failed(err: impl fmt::Display) -> Self {
688 Self::store_internal(format!("memory registry init failed: {err}"))
689 }
690
691 pub(crate) fn migration_next_step_index_u64_required(id: &str, version: u64) -> Self {
693 Self::store_internal(format!(
694 "migration '{id}@{version}' next step index does not fit persisted u64 cursor",
695 ))
696 }
697
698 pub(crate) fn recovery_integrity_validation_failed(
700 missing_index_entries: u64,
701 divergent_index_entries: u64,
702 orphan_index_references: u64,
703 ) -> Self {
704 Self::store_corruption(format!(
705 "recovery integrity validation failed: missing_index_entries={missing_index_entries} divergent_index_entries={divergent_index_entries} orphan_index_references={orphan_index_references}",
706 ))
707 }
708
709 #[cold]
711 #[inline(never)]
712 pub(crate) fn index_internal(message: impl Into<String>) -> Self {
713 Self::new(ErrorClass::Internal, ErrorOrigin::Index, message.into())
714 }
715
716 pub(crate) fn structural_index_removal_entity_key_required() -> Self {
718 Self::index_internal("missing old entity key for structural index removal")
719 }
720
721 pub(crate) fn structural_index_insertion_entity_key_required() -> Self {
723 Self::index_internal("missing new entity key for structural index insertion")
724 }
725
726 pub(crate) fn index_commit_op_old_entity_key_required() -> Self {
728 Self::index_internal("missing old entity key for index removal")
729 }
730
731 pub(crate) fn index_commit_op_new_entity_key_required() -> Self {
733 Self::index_internal("missing new entity key for index insertion")
734 }
735
736 #[cfg(test)]
738 pub(crate) fn query_internal(message: impl Into<String>) -> Self {
739 Self::new(ErrorClass::Internal, ErrorOrigin::Query, message.into())
740 }
741
742 #[cold]
744 #[inline(never)]
745 pub(crate) fn query_unsupported(message: impl Into<String>) -> Self {
746 Self::new(ErrorClass::Unsupported, ErrorOrigin::Query, message.into())
747 }
748
749 #[cold]
751 #[inline(never)]
752 pub(crate) fn serialize_internal(message: impl Into<String>) -> Self {
753 Self::new(ErrorClass::Internal, ErrorOrigin::Serialize, message.into())
754 }
755
756 pub(crate) fn persisted_row_encode_failed(detail: impl fmt::Display) -> Self {
758 Self::serialize_internal(format!("row encode failed: {detail}"))
759 }
760
761 pub(crate) fn persisted_row_field_encode_failed(
763 field_name: &str,
764 detail: impl fmt::Display,
765 ) -> Self {
766 Self::serialize_internal(format!(
767 "row encode failed for field '{field_name}': {detail}",
768 ))
769 }
770
771 pub(crate) fn bytes_field_value_encode_failed(detail: impl fmt::Display) -> Self {
773 Self::serialize_internal(format!("bytes(field) value encode failed: {detail}"))
774 }
775
776 pub(crate) fn migration_state_serialize_failed(err: impl fmt::Display) -> Self {
778 Self::serialize_internal(format!("failed to serialize migration state: {err}"))
779 }
780
781 #[cold]
783 #[inline(never)]
784 pub(crate) fn store_corruption(message: impl Into<String>) -> Self {
785 Self::new(ErrorClass::Corruption, ErrorOrigin::Store, message.into())
786 }
787
788 pub(crate) fn multiple_commit_memory_ids_registered(ids: impl fmt::Debug) -> Self {
790 Self::store_corruption(format!(
791 "multiple commit marker memory ids registered: {ids:?}"
792 ))
793 }
794
795 pub(crate) fn migration_persisted_step_index_invalid_usize(
797 id: &str,
798 version: u64,
799 step_index: u64,
800 ) -> Self {
801 Self::store_corruption(format!(
802 "migration '{id}@{version}' persisted step index does not fit runtime usize: {step_index}",
803 ))
804 }
805
806 pub(crate) fn migration_persisted_step_index_out_of_bounds(
808 id: &str,
809 version: u64,
810 step_index: usize,
811 total_steps: usize,
812 ) -> Self {
813 Self::store_corruption(format!(
814 "migration '{id}@{version}' persisted step index out of bounds: {step_index} > {total_steps}",
815 ))
816 }
817
818 pub(crate) fn commit_corruption(detail: impl fmt::Display) -> Self {
820 Self::store_corruption(format!("commit marker corrupted: {detail}"))
821 }
822
823 pub(crate) fn commit_component_corruption(component: &str, detail: impl fmt::Display) -> Self {
825 Self::store_corruption(format!("commit marker {component} corrupted: {detail}"))
826 }
827
828 pub(crate) fn commit_id_generation_failed(detail: impl fmt::Display) -> Self {
830 Self::store_internal(format!("commit id generation failed: {detail}"))
831 }
832
833 pub(crate) fn commit_marker_payload_exceeds_u32_length_limit(label: &str, len: usize) -> Self {
835 Self::store_unsupported(format!("{label} exceeds u32 length limit: {len} bytes"))
836 }
837
838 pub(crate) fn commit_component_length_invalid(
840 component: &str,
841 len: usize,
842 expected: impl fmt::Display,
843 ) -> Self {
844 Self::commit_component_corruption(
845 component,
846 format!("invalid length {len}, expected {expected}"),
847 )
848 }
849
850 pub(crate) fn commit_marker_exceeds_max_size(size: usize, max_size: u32) -> Self {
852 Self::commit_corruption(format!(
853 "commit marker exceeds max size: {size} bytes (limit {max_size})",
854 ))
855 }
856
857 pub(crate) fn commit_marker_exceeds_max_size_before_persist(
859 size: usize,
860 max_size: u32,
861 ) -> Self {
862 Self::store_unsupported(format!(
863 "commit marker exceeds max size: {size} bytes (limit {max_size})",
864 ))
865 }
866
867 pub(crate) fn commit_control_slot_exceeds_max_size(size: usize, max_size: u32) -> Self {
869 Self::store_unsupported(format!(
870 "commit control slot exceeds max size: {size} bytes (limit {max_size})",
871 ))
872 }
873
874 pub(crate) fn commit_control_slot_marker_bytes_exceed_u32_length_limit(size: usize) -> Self {
876 Self::store_unsupported(format!(
877 "commit marker bytes exceed u32 length limit: {size} bytes",
878 ))
879 }
880
881 pub(crate) fn commit_control_slot_migration_bytes_exceed_u32_length_limit(size: usize) -> Self {
883 Self::store_unsupported(format!(
884 "commit migration bytes exceed u32 length limit: {size} bytes",
885 ))
886 }
887
888 pub(crate) fn startup_index_rebuild_invalid_data_key(
890 store_path: &str,
891 detail: impl fmt::Display,
892 ) -> Self {
893 Self::store_corruption(format!(
894 "startup index rebuild failed: invalid data key in store '{store_path}' ({detail})",
895 ))
896 }
897
898 #[cold]
900 #[inline(never)]
901 pub(crate) fn index_corruption(message: impl Into<String>) -> Self {
902 Self::new(ErrorClass::Corruption, ErrorOrigin::Index, message.into())
903 }
904
905 pub(crate) fn index_unique_validation_corruption(
907 entity_path: &str,
908 fields: &str,
909 detail: impl fmt::Display,
910 ) -> Self {
911 Self::index_plan_index_corruption(format!(
912 "index corrupted: {entity_path} ({fields}) -> {detail}",
913 ))
914 }
915
916 pub(crate) fn structural_index_entry_corruption(
918 entity_path: &str,
919 fields: &str,
920 detail: impl fmt::Display,
921 ) -> Self {
922 Self::index_plan_index_corruption(format!(
923 "index corrupted: {entity_path} ({fields}) -> {detail}",
924 ))
925 }
926
927 pub(crate) fn index_unique_validation_entity_key_required() -> Self {
929 Self::index_invariant("missing entity key during unique validation")
930 }
931
932 pub(crate) fn index_unique_validation_row_deserialize_failed(
934 data_key: impl fmt::Display,
935 source: impl fmt::Display,
936 ) -> Self {
937 Self::index_plan_serialize_corruption(format!(
938 "failed to structurally deserialize row: {data_key} ({source})"
939 ))
940 }
941
942 pub(crate) fn index_unique_validation_primary_key_decode_failed(
944 data_key: impl fmt::Display,
945 source: impl fmt::Display,
946 ) -> Self {
947 Self::index_plan_serialize_corruption(format!(
948 "failed to decode structural primary-key slot: {data_key} ({source})"
949 ))
950 }
951
952 pub(crate) fn index_unique_validation_key_rebuild_failed(
954 data_key: impl fmt::Display,
955 entity_path: &str,
956 source: impl fmt::Display,
957 ) -> Self {
958 Self::index_plan_serialize_corruption(format!(
959 "failed to structurally decode unique key row {data_key} for {entity_path}: {source}",
960 ))
961 }
962
963 pub(crate) fn index_unique_validation_row_required(data_key: impl fmt::Display) -> Self {
965 Self::index_plan_store_corruption(format!("missing row: {data_key}"))
966 }
967
968 pub(crate) fn index_unique_validation_row_key_mismatch(
970 entity_path: &str,
971 fields: &str,
972 detail: impl fmt::Display,
973 ) -> Self {
974 Self::index_plan_store_invariant(format!(
975 "index invariant violated: {entity_path} ({fields}) -> {detail}",
976 ))
977 }
978
979 pub(crate) fn index_predicate_parse_failed(
981 entity_path: &str,
982 index_name: &str,
983 predicate_sql: &str,
984 err: impl fmt::Display,
985 ) -> Self {
986 Self::index_invariant(format!(
987 "index predicate parse failed: {entity_path} ({index_name}) WHERE {predicate_sql} -> {err}",
988 ))
989 }
990
991 pub(crate) fn index_only_predicate_component_required() -> Self {
993 Self::index_invariant("index-only predicate program referenced missing index component")
994 }
995
996 pub(crate) fn index_scan_continuation_anchor_within_envelope_required() -> Self {
998 Self::index_invariant(
999 "index-range continuation anchor is outside the requested range envelope",
1000 )
1001 }
1002
1003 pub(crate) fn index_scan_continuation_advancement_required() -> Self {
1005 Self::index_invariant("index-range continuation scan did not advance beyond the anchor")
1006 }
1007
1008 pub(crate) fn index_scan_key_corrupted_during(
1010 context: &'static str,
1011 err: impl fmt::Display,
1012 ) -> Self {
1013 Self::index_corruption(format!("index key corrupted during {context}: {err}"))
1014 }
1015
1016 pub(crate) fn index_projection_component_required(
1018 index_name: &str,
1019 component_index: usize,
1020 ) -> Self {
1021 Self::index_invariant(format!(
1022 "index projection referenced missing component: index='{index_name}' component_index={component_index}",
1023 ))
1024 }
1025
1026 pub(crate) fn unique_index_entry_single_key_required() -> Self {
1028 Self::index_corruption("unique index entry contains an unexpected number of keys")
1029 }
1030
1031 pub(crate) fn index_entry_decode_failed(err: impl fmt::Display) -> Self {
1033 Self::index_corruption(err.to_string())
1034 }
1035
1036 pub(crate) fn serialize_corruption(message: impl Into<String>) -> Self {
1038 Self::new(
1039 ErrorClass::Corruption,
1040 ErrorOrigin::Serialize,
1041 message.into(),
1042 )
1043 }
1044
1045 pub(crate) fn persisted_row_decode_failed(detail: impl fmt::Display) -> Self {
1047 Self::serialize_corruption(format!("row decode failed: {detail}"))
1048 }
1049
1050 pub(crate) fn persisted_row_field_decode_failed(
1052 field_name: &str,
1053 detail: impl fmt::Display,
1054 ) -> Self {
1055 Self::serialize_corruption(format!(
1056 "row decode failed for field '{field_name}': {detail}",
1057 ))
1058 }
1059
1060 pub(crate) fn persisted_row_field_kind_decode_failed(
1062 field_name: &str,
1063 field_kind: impl fmt::Debug,
1064 detail: impl fmt::Display,
1065 ) -> Self {
1066 Self::persisted_row_field_decode_failed(
1067 field_name,
1068 format!("kind={field_kind:?}: {detail}"),
1069 )
1070 }
1071
1072 pub(crate) fn persisted_row_field_payload_exact_len_required(
1074 field_name: &str,
1075 payload_kind: &str,
1076 expected_len: usize,
1077 ) -> Self {
1078 let unit = if expected_len == 1 { "byte" } else { "bytes" };
1079
1080 Self::persisted_row_field_decode_failed(
1081 field_name,
1082 format!("{payload_kind} payload must be exactly {expected_len} {unit}"),
1083 )
1084 }
1085
1086 pub(crate) fn persisted_row_field_payload_must_be_empty(
1088 field_name: &str,
1089 payload_kind: &str,
1090 ) -> Self {
1091 Self::persisted_row_field_decode_failed(
1092 field_name,
1093 format!("{payload_kind} payload must be empty"),
1094 )
1095 }
1096
1097 pub(crate) fn persisted_row_field_payload_invalid_byte(
1099 field_name: &str,
1100 payload_kind: &str,
1101 value: u8,
1102 ) -> Self {
1103 Self::persisted_row_field_decode_failed(
1104 field_name,
1105 format!("invalid {payload_kind} payload byte {value}"),
1106 )
1107 }
1108
1109 pub(crate) fn persisted_row_field_payload_non_finite(
1111 field_name: &str,
1112 payload_kind: &str,
1113 ) -> Self {
1114 Self::persisted_row_field_decode_failed(
1115 field_name,
1116 format!("{payload_kind} payload is non-finite"),
1117 )
1118 }
1119
1120 pub(crate) fn persisted_row_field_payload_out_of_range(
1122 field_name: &str,
1123 payload_kind: &str,
1124 ) -> Self {
1125 Self::persisted_row_field_decode_failed(
1126 field_name,
1127 format!("{payload_kind} payload out of range for target type"),
1128 )
1129 }
1130
1131 pub(crate) fn persisted_row_field_text_payload_invalid_utf8(
1133 field_name: &str,
1134 detail: impl fmt::Display,
1135 ) -> Self {
1136 Self::persisted_row_field_decode_failed(
1137 field_name,
1138 format!("invalid UTF-8 text payload ({detail})"),
1139 )
1140 }
1141
1142 pub(crate) fn persisted_row_slot_lookup_out_of_bounds(model_path: &str, slot: usize) -> Self {
1144 Self::index_invariant(format!(
1145 "slot lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1146 ))
1147 }
1148
1149 pub(crate) fn persisted_row_slot_cache_lookup_out_of_bounds(
1151 model_path: &str,
1152 slot: usize,
1153 ) -> Self {
1154 Self::index_invariant(format!(
1155 "slot cache lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1156 ))
1157 }
1158
1159 pub(crate) fn persisted_row_primary_key_field_missing(model_path: &str) -> Self {
1161 Self::index_invariant(format!(
1162 "entity primary key field missing during structural row validation: {model_path}",
1163 ))
1164 }
1165
1166 pub(crate) fn persisted_row_primary_key_not_storage_encodable(
1168 data_key: impl fmt::Display,
1169 detail: impl fmt::Display,
1170 ) -> Self {
1171 Self::persisted_row_decode_failed(format!(
1172 "primary-key value is not storage-key encodable: {data_key} ({detail})",
1173 ))
1174 }
1175
1176 pub(crate) fn persisted_row_primary_key_slot_missing(data_key: impl fmt::Display) -> Self {
1178 Self::persisted_row_decode_failed(format!(
1179 "missing primary-key slot while validating {data_key}",
1180 ))
1181 }
1182
1183 pub(crate) fn persisted_row_key_mismatch(
1185 expected_key: impl fmt::Display,
1186 found_key: impl fmt::Display,
1187 ) -> Self {
1188 Self::store_corruption(format!(
1189 "row key mismatch: expected {expected_key}, found {found_key}",
1190 ))
1191 }
1192
1193 pub(crate) fn row_decode_declared_field_missing(field_name: &str) -> Self {
1195 Self::persisted_row_decode_failed(format!("missing declared field `{field_name}`"))
1196 }
1197
1198 pub(crate) fn row_decode_field_decode_failed(
1200 field_name: &str,
1201 field_kind: impl fmt::Debug,
1202 detail: impl fmt::Display,
1203 ) -> Self {
1204 Self::serialize_corruption(format!(
1205 "row decode failed for field '{field_name}' kind={field_kind:?}: {detail}",
1206 ))
1207 }
1208
1209 pub(crate) fn row_decode_primary_key_slot_missing() -> Self {
1211 Self::persisted_row_decode_failed("missing primary-key slot value")
1212 }
1213
1214 pub(crate) fn row_decode_primary_key_not_storage_encodable(detail: impl fmt::Display) -> Self {
1216 Self::persisted_row_decode_failed(format!(
1217 "primary-key value is not storage-key encodable: {detail}",
1218 ))
1219 }
1220
1221 pub(crate) fn row_decode_key_mismatch(
1223 expected_key: impl fmt::Display,
1224 found_key: impl fmt::Display,
1225 ) -> Self {
1226 Self::persisted_row_key_mismatch(expected_key, found_key)
1227 }
1228
1229 pub(crate) fn data_key_entity_mismatch(
1231 expected: impl fmt::Display,
1232 found: impl fmt::Display,
1233 ) -> Self {
1234 Self::store_corruption(format!(
1235 "data key entity mismatch: expected {expected}, found {found}",
1236 ))
1237 }
1238
1239 pub(crate) fn data_key_primary_key_decode_failed(value: impl fmt::Debug) -> Self {
1241 Self::store_corruption(format!("data key primary key decode failed: {value:?}",))
1242 }
1243
1244 pub(crate) fn reverse_index_ordinal_overflow(
1246 source_path: &str,
1247 field_name: &str,
1248 target_path: &str,
1249 detail: impl fmt::Display,
1250 ) -> Self {
1251 Self::index_internal(format!(
1252 "reverse index ordinal overflow: source={source_path} field={field_name} target={target_path} ({detail})",
1253 ))
1254 }
1255
1256 pub(crate) fn reverse_index_entry_corrupted(
1258 source_path: &str,
1259 field_name: &str,
1260 target_path: &str,
1261 index_key: impl fmt::Debug,
1262 detail: impl fmt::Display,
1263 ) -> Self {
1264 Self::index_corruption(format!(
1265 "reverse index entry corrupted: source={source_path} field={field_name} target={target_path} key={index_key:?} ({detail})",
1266 ))
1267 }
1268
1269 pub(crate) fn reverse_index_entry_encode_failed(
1271 source_path: &str,
1272 field_name: &str,
1273 target_path: &str,
1274 detail: impl fmt::Display,
1275 ) -> Self {
1276 Self::index_unsupported(format!(
1277 "reverse index entry encoding failed: source={source_path} field={field_name} target={target_path} ({detail})",
1278 ))
1279 }
1280
1281 pub(crate) fn relation_target_store_missing(
1283 source_path: &str,
1284 field_name: &str,
1285 target_path: &str,
1286 store_path: &str,
1287 detail: impl fmt::Display,
1288 ) -> Self {
1289 Self::executor_internal(format!(
1290 "relation target store missing: source={source_path} field={field_name} target={target_path} store={store_path} ({detail})",
1291 ))
1292 }
1293
1294 pub(crate) fn relation_target_key_decode_failed(
1296 context_label: &str,
1297 source_path: &str,
1298 field_name: &str,
1299 target_path: &str,
1300 detail: impl fmt::Display,
1301 ) -> Self {
1302 Self::identity_corruption(format!(
1303 "{context_label}: source={source_path} field={field_name} target={target_path} ({detail})",
1304 ))
1305 }
1306
1307 pub(crate) fn relation_target_entity_mismatch(
1309 context_label: &str,
1310 source_path: &str,
1311 field_name: &str,
1312 target_path: &str,
1313 target_entity_name: &str,
1314 expected_tag: impl fmt::Display,
1315 actual_tag: impl fmt::Display,
1316 ) -> Self {
1317 Self::store_corruption(format!(
1318 "{context_label}: source={source_path} field={field_name} target={target_path} expected={target_entity_name} (tag={expected_tag}) actual_tag={actual_tag}",
1319 ))
1320 }
1321
1322 pub(crate) fn relation_source_row_missing_field(
1324 source_path: &str,
1325 field_name: &str,
1326 target_path: &str,
1327 ) -> Self {
1328 Self::serialize_corruption(format!(
1329 "relation source row decode failed: missing field: source={source_path} field={field_name} target={target_path}",
1330 ))
1331 }
1332
1333 pub(crate) fn relation_source_row_decode_failed(
1335 source_path: &str,
1336 field_name: &str,
1337 target_path: &str,
1338 detail: impl fmt::Display,
1339 ) -> Self {
1340 Self::serialize_corruption(format!(
1341 "relation source row decode failed: source={source_path} field={field_name} target={target_path} ({detail})",
1342 ))
1343 }
1344
1345 pub(crate) fn relation_source_row_unsupported_scalar_relation_key(
1347 source_path: &str,
1348 field_name: &str,
1349 target_path: &str,
1350 ) -> Self {
1351 Self::serialize_corruption(format!(
1352 "relation source row decode failed: unsupported scalar relation key: source={source_path} field={field_name} target={target_path}",
1353 ))
1354 }
1355
1356 pub(crate) fn relation_source_row_invalid_field_kind(field_kind: impl fmt::Debug) -> Self {
1358 Self::serialize_corruption(format!(
1359 "invalid strong relation field kind during structural decode: {field_kind:?}"
1360 ))
1361 }
1362
1363 pub(crate) fn relation_source_row_unsupported_key_kind(field_kind: impl fmt::Debug) -> Self {
1365 Self::serialize_corruption(format!(
1366 "unsupported strong relation key kind during structural decode: {field_kind:?}"
1367 ))
1368 }
1369
1370 pub(crate) fn reverse_index_relation_target_decode_invariant_violated(
1372 source_path: &str,
1373 field_name: &str,
1374 target_path: &str,
1375 ) -> Self {
1376 Self::executor_internal(format!(
1377 "relation target decode invariant violated while preparing reverse index: source={source_path} field={field_name} target={target_path}",
1378 ))
1379 }
1380
1381 pub(crate) fn bytes_covering_component_payload_empty() -> Self {
1383 Self::index_corruption("index component payload is empty during covering projection decode")
1384 }
1385
1386 pub(crate) fn bytes_covering_bool_payload_truncated() -> Self {
1388 Self::index_corruption("bool covering component payload is truncated")
1389 }
1390
1391 pub(crate) fn bytes_covering_component_payload_invalid_length(payload_kind: &str) -> Self {
1393 Self::index_corruption(format!(
1394 "{payload_kind} covering component payload has invalid length"
1395 ))
1396 }
1397
1398 pub(crate) fn bytes_covering_bool_payload_invalid_value() -> Self {
1400 Self::index_corruption("bool covering component payload has invalid value")
1401 }
1402
1403 pub(crate) fn bytes_covering_text_payload_invalid_terminator() -> Self {
1405 Self::index_corruption("text covering component payload has invalid terminator")
1406 }
1407
1408 pub(crate) fn bytes_covering_text_payload_trailing_bytes() -> Self {
1410 Self::index_corruption("text covering component payload contains trailing bytes")
1411 }
1412
1413 pub(crate) fn bytes_covering_text_payload_invalid_utf8() -> Self {
1415 Self::index_corruption("text covering component payload is not valid UTF-8")
1416 }
1417
1418 pub(crate) fn bytes_covering_text_payload_invalid_escape_byte() -> Self {
1420 Self::index_corruption("text covering component payload has invalid escape byte")
1421 }
1422
1423 pub(crate) fn bytes_covering_text_payload_missing_terminator() -> Self {
1425 Self::index_corruption("text covering component payload is missing terminator")
1426 }
1427
1428 #[must_use]
1430 pub fn missing_persisted_slot(field_name: &'static str) -> Self {
1431 Self::serialize_corruption(format!(
1432 "row decode failed: missing required field '{field_name}'",
1433 ))
1434 }
1435
1436 pub(crate) fn identity_corruption(message: impl Into<String>) -> Self {
1438 Self::new(
1439 ErrorClass::Corruption,
1440 ErrorOrigin::Identity,
1441 message.into(),
1442 )
1443 }
1444
1445 #[cold]
1447 #[inline(never)]
1448 pub(crate) fn store_unsupported(message: impl Into<String>) -> Self {
1449 Self::new(ErrorClass::Unsupported, ErrorOrigin::Store, message.into())
1450 }
1451
1452 pub(crate) fn migration_label_empty(label: &str) -> Self {
1454 Self::store_unsupported(format!("{label} cannot be empty"))
1455 }
1456
1457 pub(crate) fn migration_step_row_ops_required(name: &str) -> Self {
1459 Self::store_unsupported(format!(
1460 "migration step '{name}' must include at least one row op",
1461 ))
1462 }
1463
1464 pub(crate) fn migration_plan_version_required(id: &str) -> Self {
1466 Self::store_unsupported(format!("migration plan '{id}' version must be > 0",))
1467 }
1468
1469 pub(crate) fn migration_plan_steps_required(id: &str) -> Self {
1471 Self::store_unsupported(format!(
1472 "migration plan '{id}' must include at least one step",
1473 ))
1474 }
1475
1476 pub(crate) fn migration_cursor_out_of_bounds(
1478 id: &str,
1479 version: u64,
1480 next_step: usize,
1481 total_steps: usize,
1482 ) -> Self {
1483 Self::store_unsupported(format!(
1484 "migration '{id}@{version}' cursor out of bounds: next_step={next_step} total_steps={total_steps}",
1485 ))
1486 }
1487
1488 pub(crate) fn migration_execution_requires_max_steps(id: &str) -> Self {
1490 Self::store_unsupported(format!("migration '{id}' execution requires max_steps > 0",))
1491 }
1492
1493 pub(crate) fn migration_in_progress_conflict(
1495 requested_id: &str,
1496 requested_version: u64,
1497 active_id: &str,
1498 active_version: u64,
1499 ) -> Self {
1500 Self::store_unsupported(format!(
1501 "migration '{requested_id}@{requested_version}' cannot execute while migration '{active_id}@{active_version}' is in progress",
1502 ))
1503 }
1504
1505 pub(crate) fn unsupported_entity_tag_in_data_store(
1507 entity_tag: crate::types::EntityTag,
1508 ) -> Self {
1509 Self::store_unsupported(format!(
1510 "unsupported entity tag in data store: '{}'",
1511 entity_tag.value()
1512 ))
1513 }
1514
1515 pub(crate) fn configured_commit_memory_id_mismatch(
1517 configured_id: u8,
1518 registered_id: u8,
1519 ) -> Self {
1520 Self::store_unsupported(format!(
1521 "configured commit memory id {configured_id} does not match existing commit marker id {registered_id}",
1522 ))
1523 }
1524
1525 pub(crate) fn commit_memory_id_already_registered(memory_id: u8, label: &str) -> Self {
1527 Self::store_unsupported(format!(
1528 "configured commit memory id {memory_id} is already registered as '{label}'",
1529 ))
1530 }
1531
1532 pub(crate) fn commit_memory_id_outside_reserved_ranges(memory_id: u8) -> Self {
1534 Self::store_unsupported(format!(
1535 "configured commit memory id {memory_id} is outside reserved ranges",
1536 ))
1537 }
1538
1539 pub(crate) fn commit_memory_id_registration_failed(err: impl fmt::Display) -> Self {
1541 Self::store_internal(format!("commit memory id registration failed: {err}"))
1542 }
1543
1544 pub(crate) fn index_unsupported(message: impl Into<String>) -> Self {
1546 Self::new(ErrorClass::Unsupported, ErrorOrigin::Index, message.into())
1547 }
1548
1549 pub(crate) fn index_component_exceeds_max_size(
1551 key_item: impl fmt::Display,
1552 len: usize,
1553 max_component_size: usize,
1554 ) -> Self {
1555 Self::index_unsupported(format!(
1556 "index component exceeds max size: key item '{key_item}' -> {len} bytes (limit {max_component_size})",
1557 ))
1558 }
1559
1560 pub(crate) fn index_entry_exceeds_max_keys(
1562 entity_path: &str,
1563 fields: &str,
1564 keys: usize,
1565 ) -> Self {
1566 Self::index_unsupported(format!(
1567 "index entry exceeds max keys: {entity_path} ({fields}) -> {keys} keys",
1568 ))
1569 }
1570
1571 pub(crate) fn index_entry_duplicate_keys_unexpected(entity_path: &str, fields: &str) -> Self {
1573 Self::index_invariant(format!(
1574 "index entry unexpectedly contains duplicate keys: {entity_path} ({fields})",
1575 ))
1576 }
1577
1578 pub(crate) fn index_entry_key_encoding_failed(
1580 entity_path: &str,
1581 fields: &str,
1582 err: impl fmt::Display,
1583 ) -> Self {
1584 Self::index_unsupported(format!(
1585 "index entry key encoding failed: {entity_path} ({fields}) -> {err}",
1586 ))
1587 }
1588
1589 pub(crate) fn serialize_unsupported(message: impl Into<String>) -> Self {
1591 Self::new(
1592 ErrorClass::Unsupported,
1593 ErrorOrigin::Serialize,
1594 message.into(),
1595 )
1596 }
1597
1598 pub(crate) fn cursor_unsupported(message: impl Into<String>) -> Self {
1600 Self::new(ErrorClass::Unsupported, ErrorOrigin::Cursor, message.into())
1601 }
1602
1603 pub(crate) fn serialize_incompatible_persisted_format(message: impl Into<String>) -> Self {
1605 Self::new(
1606 ErrorClass::IncompatiblePersistedFormat,
1607 ErrorOrigin::Serialize,
1608 message.into(),
1609 )
1610 }
1611
1612 pub(crate) fn serialize_payload_decode_failed(
1615 source: SerializeError,
1616 payload_label: &'static str,
1617 ) -> Self {
1618 match source {
1619 SerializeError::DeserializeSizeLimitExceeded { len, max_bytes } => {
1622 Self::serialize_corruption(format!(
1623 "{payload_label} decode failed: payload size {len} exceeds limit {max_bytes}"
1624 ))
1625 }
1626 SerializeError::Deserialize(_) => Self::serialize_corruption(format!(
1627 "{payload_label} decode failed: {}",
1628 SerializeErrorKind::Deserialize
1629 )),
1630 SerializeError::Serialize(_) => Self::serialize_corruption(format!(
1631 "{payload_label} decode failed: {}",
1632 SerializeErrorKind::Serialize
1633 )),
1634 }
1635 }
1636
1637 #[cfg(feature = "sql")]
1640 pub(crate) fn query_unsupported_sql_feature(feature: &'static str) -> Self {
1641 let message = format!(
1642 "SQL query is not executable in this release: unsupported SQL feature: {feature}"
1643 );
1644
1645 Self {
1646 class: ErrorClass::Unsupported,
1647 origin: ErrorOrigin::Query,
1648 message,
1649 detail: Some(ErrorDetail::Query(
1650 QueryErrorDetail::UnsupportedSqlFeature { feature },
1651 )),
1652 }
1653 }
1654
1655 pub fn store_not_found(key: impl Into<String>) -> Self {
1656 let key = key.into();
1657
1658 Self {
1659 class: ErrorClass::NotFound,
1660 origin: ErrorOrigin::Store,
1661 message: format!("data key not found: {key}"),
1662 detail: Some(ErrorDetail::Store(StoreError::NotFound { key })),
1663 }
1664 }
1665
1666 pub fn unsupported_entity_path(path: impl Into<String>) -> Self {
1668 let path = path.into();
1669
1670 Self::new(
1671 ErrorClass::Unsupported,
1672 ErrorOrigin::Store,
1673 format!("unsupported entity path: '{path}'"),
1674 )
1675 }
1676
1677 #[must_use]
1678 pub const fn is_not_found(&self) -> bool {
1679 matches!(
1680 self.detail,
1681 Some(ErrorDetail::Store(StoreError::NotFound { .. }))
1682 )
1683 }
1684
1685 #[must_use]
1686 pub fn display_with_class(&self) -> String {
1687 format!("{}:{}: {}", self.origin, self.class, self.message)
1688 }
1689
1690 #[cold]
1692 #[inline(never)]
1693 pub(crate) fn index_plan_corruption(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1694 let message = message.into();
1695 Self::new(
1696 ErrorClass::Corruption,
1697 origin,
1698 format!("corruption detected ({origin}): {message}"),
1699 )
1700 }
1701
1702 #[cold]
1704 #[inline(never)]
1705 pub(crate) fn index_plan_index_corruption(message: impl Into<String>) -> Self {
1706 Self::index_plan_corruption(ErrorOrigin::Index, message)
1707 }
1708
1709 #[cold]
1711 #[inline(never)]
1712 pub(crate) fn index_plan_store_corruption(message: impl Into<String>) -> Self {
1713 Self::index_plan_corruption(ErrorOrigin::Store, message)
1714 }
1715
1716 #[cold]
1718 #[inline(never)]
1719 pub(crate) fn index_plan_serialize_corruption(message: impl Into<String>) -> Self {
1720 Self::index_plan_corruption(ErrorOrigin::Serialize, message)
1721 }
1722
1723 pub(crate) fn index_plan_invariant(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1725 let message = message.into();
1726 Self::new(
1727 ErrorClass::InvariantViolation,
1728 origin,
1729 format!("invariant violation detected ({origin}): {message}"),
1730 )
1731 }
1732
1733 pub(crate) fn index_plan_store_invariant(message: impl Into<String>) -> Self {
1735 Self::index_plan_invariant(ErrorOrigin::Store, message)
1736 }
1737
1738 pub(crate) fn index_violation(path: &str, index_fields: &[&str]) -> Self {
1740 Self::new(
1741 ErrorClass::Conflict,
1742 ErrorOrigin::Index,
1743 format!(
1744 "index constraint violation: {path} ({})",
1745 index_fields.join(", ")
1746 ),
1747 )
1748 }
1749}
1750
1751#[derive(Debug, ThisError)]
1759pub enum ErrorDetail {
1760 #[error("{0}")]
1761 Store(StoreError),
1762 #[error("{0}")]
1763 Query(QueryErrorDetail),
1764 }
1771
1772#[derive(Debug, ThisError)]
1780pub enum StoreError {
1781 #[error("key not found: {key}")]
1782 NotFound { key: String },
1783
1784 #[error("store corruption: {message}")]
1785 Corrupt { message: String },
1786
1787 #[error("store invariant violation: {message}")]
1788 InvariantViolation { message: String },
1789}
1790
1791#[derive(Debug, ThisError)]
1798pub enum QueryErrorDetail {
1799 #[error("unsupported SQL feature: {feature}")]
1800 UnsupportedSqlFeature { feature: &'static str },
1801}
1802
1803#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1810pub enum ErrorClass {
1811 Corruption,
1812 IncompatiblePersistedFormat,
1813 NotFound,
1814 Internal,
1815 Conflict,
1816 Unsupported,
1817 InvariantViolation,
1818}
1819
1820impl fmt::Display for ErrorClass {
1821 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1822 let label = match self {
1823 Self::Corruption => "corruption",
1824 Self::IncompatiblePersistedFormat => "incompatible_persisted_format",
1825 Self::NotFound => "not_found",
1826 Self::Internal => "internal",
1827 Self::Conflict => "conflict",
1828 Self::Unsupported => "unsupported",
1829 Self::InvariantViolation => "invariant_violation",
1830 };
1831 write!(f, "{label}")
1832 }
1833}
1834
1835#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1842pub enum ErrorOrigin {
1843 Serialize,
1844 Store,
1845 Index,
1846 Identity,
1847 Query,
1848 Planner,
1849 Cursor,
1850 Recovery,
1851 Response,
1852 Executor,
1853 Interface,
1854}
1855
1856impl fmt::Display for ErrorOrigin {
1857 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1858 let label = match self {
1859 Self::Serialize => "serialize",
1860 Self::Store => "store",
1861 Self::Index => "index",
1862 Self::Identity => "identity",
1863 Self::Query => "query",
1864 Self::Planner => "planner",
1865 Self::Cursor => "cursor",
1866 Self::Recovery => "recovery",
1867 Self::Response => "response",
1868 Self::Executor => "executor",
1869 Self::Interface => "interface",
1870 };
1871 write!(f, "{label}")
1872 }
1873}