1#[cfg(test)]
9mod tests;
10
11use icydb_diagnostic_code as diagnostic_code;
12use std::fmt;
13use thiserror::Error as ThisError;
14
15#[derive(Debug, ThisError)]
116#[error("{message}")]
117pub struct InternalError {
118 pub(crate) class: ErrorClass,
119 pub(crate) origin: ErrorOrigin,
120 pub(crate) message: String,
121
122 pub(crate) detail: Option<ErrorDetail>,
125}
126
127impl InternalError {
128 #[cold]
132 #[inline(never)]
133 pub fn new(class: ErrorClass, origin: ErrorOrigin, message: impl Into<String>) -> Self {
134 let message = message.into();
135
136 let detail = match (class, origin) {
137 (ErrorClass::Corruption, ErrorOrigin::Store) => {
138 Some(ErrorDetail::Store(StoreError::Corrupt {
139 message: message.clone(),
140 }))
141 }
142 (ErrorClass::InvariantViolation, ErrorOrigin::Store) => {
143 Some(ErrorDetail::Store(StoreError::InvariantViolation {
144 message: message.clone(),
145 }))
146 }
147 _ => None,
148 };
149
150 Self {
151 class,
152 origin,
153 message,
154 detail,
155 }
156 }
157
158 #[must_use]
160 pub const fn class(&self) -> ErrorClass {
161 self.class
162 }
163
164 #[must_use]
166 pub const fn origin(&self) -> ErrorOrigin {
167 self.origin
168 }
169
170 #[must_use]
172 pub fn message(&self) -> &str {
173 &self.message
174 }
175
176 #[must_use]
178 pub const fn detail(&self) -> Option<&ErrorDetail> {
179 self.detail.as_ref()
180 }
181
182 #[must_use]
184 pub fn diagnostic(&self) -> diagnostic_code::Diagnostic {
185 diagnostic_code::Diagnostic::new(
186 self.diagnostic_code(),
187 self.origin.diagnostic_origin(),
188 self.detail
189 .as_ref()
190 .and_then(ErrorDetail::diagnostic_detail),
191 )
192 }
193
194 #[must_use]
196 pub fn diagnostic_code(&self) -> diagnostic_code::DiagnosticCode {
197 self.detail.as_ref().map_or_else(
198 || self.class.diagnostic_code(self.origin),
199 ErrorDetail::diagnostic_code,
200 )
201 }
202
203 #[must_use]
205 pub fn into_message(self) -> String {
206 self.message
207 }
208
209 #[cold]
211 #[inline(never)]
212 pub(crate) fn classified(
213 class: ErrorClass,
214 origin: ErrorOrigin,
215 message: impl Into<String>,
216 ) -> Self {
217 Self::new(class, origin, message)
218 }
219
220 #[cold]
222 #[inline(never)]
223 pub(crate) fn with_message(self, message: impl Into<String>) -> Self {
224 Self::classified(self.class, self.origin, message)
225 }
226
227 #[cold]
231 #[inline(never)]
232 pub(crate) fn with_origin(self, origin: ErrorOrigin) -> Self {
233 Self::classified(self.class, origin, self.message)
234 }
235
236 #[cold]
238 #[inline(never)]
239 pub(crate) fn index_invariant(message: impl Into<String>) -> Self {
240 Self::new(
241 ErrorClass::InvariantViolation,
242 ErrorOrigin::Index,
243 message.into(),
244 )
245 }
246
247 pub(crate) fn index_key_field_count_exceeds_max(
249 index_name: &str,
250 field_count: usize,
251 max_fields: usize,
252 ) -> Self {
253 Self::index_invariant(format!(
254 "index '{index_name}' has {field_count} fields (max {max_fields})",
255 ))
256 }
257
258 pub(crate) fn index_key_item_field_missing_on_entity_model(field: &str) -> Self {
260 Self::index_invariant(format!(
261 "index key item field missing on entity model: {field}",
262 ))
263 }
264
265 pub(crate) fn index_key_item_field_missing_on_lookup_row(field: &str) -> Self {
267 Self::index_invariant(format!(
268 "index key item field missing on lookup row: {field}",
269 ))
270 }
271
272 pub(crate) fn index_expression_source_type_mismatch(
274 index_name: &str,
275 expression: impl fmt::Display,
276 expected: &str,
277 source_label: &str,
278 ) -> Self {
279 Self::index_invariant(format!(
280 "index '{index_name}' expression '{expression}' expected {expected} source value, got {source_label}",
281 ))
282 }
283
284 #[cold]
287 #[inline(never)]
288 pub(crate) fn planner_executor_invariant(reason: impl Into<String>) -> Self {
289 Self::new(
290 ErrorClass::InvariantViolation,
291 ErrorOrigin::Planner,
292 Self::executor_invariant_message(reason),
293 )
294 }
295
296 #[cold]
299 #[inline(never)]
300 pub(crate) fn query_executor_invariant(reason: impl Into<String>) -> Self {
301 Self::new(
302 ErrorClass::InvariantViolation,
303 ErrorOrigin::Query,
304 Self::executor_invariant_message(reason),
305 )
306 }
307
308 #[cold]
311 #[inline(never)]
312 pub(crate) fn cursor_executor_invariant(reason: impl Into<String>) -> Self {
313 Self::new(
314 ErrorClass::InvariantViolation,
315 ErrorOrigin::Cursor,
316 Self::executor_invariant_message(reason),
317 )
318 }
319
320 #[cold]
322 #[inline(never)]
323 pub(crate) fn executor_invariant(message: impl Into<String>) -> Self {
324 Self::new(
325 ErrorClass::InvariantViolation,
326 ErrorOrigin::Executor,
327 message.into(),
328 )
329 }
330
331 #[cold]
333 #[inline(never)]
334 pub(crate) fn executor_internal(message: impl Into<String>) -> Self {
335 Self::new(ErrorClass::Internal, ErrorOrigin::Executor, message.into())
336 }
337
338 #[cold]
340 #[inline(never)]
341 pub(crate) fn executor_unsupported(message: impl Into<String>) -> Self {
342 Self::new(
343 ErrorClass::Unsupported,
344 ErrorOrigin::Executor,
345 message.into(),
346 )
347 }
348
349 pub(crate) fn mutation_entity_primary_key_missing(entity_path: &str, field_name: &str) -> Self {
351 Self::executor_invariant(format!(
352 "entity primary key field missing: {entity_path} field={field_name}",
353 ))
354 }
355
356 pub(crate) fn mutation_entity_primary_key_invalid_value(
358 entity_path: &str,
359 field_name: &str,
360 value: &crate::value::Value,
361 ) -> Self {
362 Self::executor_invariant(format!(
363 "entity primary key field has invalid value: {entity_path} field={field_name} value={value:?}",
364 ))
365 }
366
367 pub(crate) fn mutation_entity_primary_key_type_mismatch(
369 entity_path: &str,
370 field_name: &str,
371 value: &crate::value::Value,
372 ) -> Self {
373 Self::executor_invariant(format!(
374 "entity primary key field type mismatch: {entity_path} field={field_name} value={value:?}",
375 ))
376 }
377
378 pub(crate) fn mutation_entity_primary_key_mismatch(
380 entity_path: &str,
381 field_name: &str,
382 field_value: &crate::value::Value,
383 identity_key: &crate::value::Value,
384 ) -> Self {
385 Self::executor_invariant(format!(
386 "entity primary key mismatch: {entity_path} field={field_name} field_value={field_value:?} id_key={identity_key:?}",
387 ))
388 }
389
390 pub(crate) fn mutation_entity_field_missing(
392 entity_path: &str,
393 field_name: &str,
394 indexed: bool,
395 ) -> Self {
396 let indexed_note = if indexed { " (indexed)" } else { "" };
397
398 Self::executor_invariant(format!(
399 "entity field missing: {entity_path} field={field_name}{indexed_note}",
400 ))
401 }
402
403 pub(crate) fn mutation_structural_patch_required_field_missing(
405 entity_path: &str,
406 field_name: &str,
407 ) -> Self {
408 Self::executor_invariant(format!(
409 "structural patch missing required field: {entity_path} field={field_name}",
410 ))
411 }
412
413 pub(crate) fn mutation_entity_field_type_mismatch(
415 entity_path: &str,
416 field_name: &str,
417 value: &crate::value::Value,
418 ) -> Self {
419 Self::executor_invariant(format!(
420 "entity field type mismatch: {entity_path} field={field_name} value={value:?}",
421 ))
422 }
423
424 pub(crate) fn mutation_generated_field_explicit(entity_path: &str, field_name: &str) -> Self {
426 Self::executor_unsupported(format!(
427 "generated field may not be explicitly written: {entity_path} field={field_name}",
428 ))
429 }
430
431 #[must_use]
433 pub fn mutation_create_missing_authored_fields(entity_path: &str, field_names: &str) -> Self {
434 Self::executor_unsupported(format!(
435 "create requires explicit values for authorable fields {field_names}: {entity_path}",
436 ))
437 }
438
439 pub(crate) fn mutation_structural_after_image_invalid(
444 entity_path: &str,
445 data_key: impl fmt::Display,
446 detail: impl AsRef<str>,
447 ) -> Self {
448 Self::executor_invariant(format!(
449 "mutation result is invalid: {entity_path} key={data_key} ({})",
450 detail.as_ref(),
451 ))
452 }
453
454 pub(crate) fn mutation_structural_field_unknown(entity_path: &str, field_name: &str) -> Self {
456 Self::executor_invariant(format!(
457 "mutation field not found: {entity_path} field={field_name}",
458 ))
459 }
460
461 pub(crate) fn mutation_decimal_scale_mismatch(
463 entity_path: &str,
464 field_name: &str,
465 expected_scale: impl fmt::Display,
466 actual_scale: impl fmt::Display,
467 ) -> Self {
468 Self::executor_unsupported(format!(
469 "decimal field scale mismatch: {entity_path} field={field_name} expected_scale={expected_scale} actual_scale={actual_scale}",
470 ))
471 }
472
473 pub(crate) fn mutation_text_max_len_exceeded(
475 entity_path: &str,
476 field_name: &str,
477 max_len: impl fmt::Display,
478 actual_len: impl fmt::Display,
479 ) -> Self {
480 Self::executor_unsupported(format!(
481 "text length exceeds max_len: {entity_path} field={field_name} max_len={max_len} actual_len={actual_len}",
482 ))
483 }
484
485 pub(crate) fn mutation_set_field_list_required(entity_path: &str, field_name: &str) -> Self {
487 Self::executor_invariant(format!(
488 "set field must encode as Value::List: {entity_path} field={field_name}",
489 ))
490 }
491
492 pub(crate) fn mutation_set_field_not_canonical(entity_path: &str, field_name: &str) -> Self {
494 Self::executor_invariant(format!(
495 "set field must be strictly ordered and deduplicated: {entity_path} field={field_name}",
496 ))
497 }
498
499 pub(crate) fn mutation_map_field_map_required(entity_path: &str, field_name: &str) -> Self {
501 Self::executor_invariant(format!(
502 "map field must encode as Value::Map: {entity_path} field={field_name}",
503 ))
504 }
505
506 pub(crate) fn mutation_map_field_entries_invalid(
508 entity_path: &str,
509 field_name: &str,
510 detail: impl fmt::Display,
511 ) -> Self {
512 Self::executor_invariant(format!(
513 "map field entries violate map invariants: {entity_path} field={field_name} ({detail})",
514 ))
515 }
516
517 pub(crate) fn mutation_map_field_entries_not_canonical(
519 entity_path: &str,
520 field_name: &str,
521 ) -> Self {
522 Self::executor_invariant(format!(
523 "map field entries are not in canonical deterministic order: {entity_path} field={field_name}",
524 ))
525 }
526
527 pub(crate) fn scalar_page_ordering_after_filtering_required() -> Self {
529 Self::query_executor_invariant("ordering must run after filtering")
530 }
531
532 pub(crate) fn scalar_page_cursor_boundary_order_required() -> Self {
534 Self::query_executor_invariant("cursor boundary requires ordering")
535 }
536
537 pub(crate) fn scalar_page_cursor_boundary_after_ordering_required() -> Self {
539 Self::query_executor_invariant("cursor boundary must run after ordering")
540 }
541
542 pub(crate) fn scalar_page_pagination_after_ordering_required() -> Self {
544 Self::query_executor_invariant("pagination must run after ordering")
545 }
546
547 pub(crate) fn scalar_page_delete_limit_after_ordering_required() -> Self {
549 Self::query_executor_invariant("delete limit must run after ordering")
550 }
551
552 pub(crate) fn load_runtime_scalar_payload_required() -> Self {
554 Self::query_executor_invariant("scalar load mode must carry scalar runtime payload")
555 }
556
557 pub(crate) fn load_runtime_grouped_payload_required() -> Self {
559 Self::query_executor_invariant("grouped load mode must carry grouped runtime payload")
560 }
561
562 pub(crate) fn load_runtime_scalar_surface_payload_required() -> Self {
564 Self::query_executor_invariant("scalar page load mode must carry scalar runtime payload")
565 }
566
567 pub(crate) fn load_runtime_grouped_surface_payload_required() -> Self {
569 Self::query_executor_invariant("grouped page load mode must carry grouped runtime payload")
570 }
571
572 pub(crate) fn load_executor_load_plan_required() -> Self {
574 Self::query_executor_invariant("load executor requires load plans")
575 }
576
577 pub(crate) fn delete_executor_grouped_unsupported() -> Self {
579 Self::executor_unsupported("grouped query execution is not yet enabled in this release")
580 }
581
582 pub(crate) fn delete_executor_delete_plan_required() -> Self {
584 Self::query_executor_invariant("delete executor requires delete plans")
585 }
586
587 pub(crate) fn aggregate_fold_mode_terminal_contract_required() -> Self {
589 Self::query_executor_invariant(
590 "aggregate fold mode must match route fold-mode contract for aggregate terminal",
591 )
592 }
593
594 pub(crate) fn fast_stream_route_kind_request_match_required() -> Self {
596 Self::query_executor_invariant("fast-stream route kind/request mismatch")
597 }
598
599 pub(crate) fn secondary_index_prefix_spec_required() -> Self {
601 Self::query_executor_invariant(
602 "index-prefix executable spec must be materialized for index-prefix plans",
603 )
604 }
605
606 pub(crate) fn index_range_limit_spec_required() -> Self {
608 Self::query_executor_invariant(
609 "index-range executable spec must be materialized for index-range plans",
610 )
611 }
612
613 pub(crate) fn mutation_atomic_save_duplicate_key(
615 entity_path: &str,
616 key: impl fmt::Display,
617 ) -> Self {
618 Self::executor_unsupported(format!(
619 "atomic save batch rejected duplicate key: entity={entity_path} key={key}",
620 ))
621 }
622
623 pub(crate) fn mutation_index_store_generation_changed(
625 expected_generation: u64,
626 observed_generation: u64,
627 ) -> Self {
628 Self::executor_invariant(format!(
629 "index store generation changed between preflight and apply: expected {expected_generation}, found {observed_generation}",
630 ))
631 }
632
633 #[must_use]
635 #[cold]
636 #[inline(never)]
637 pub(crate) fn executor_invariant_message(reason: impl Into<String>) -> String {
638 format!("executor invariant violated: {}", reason.into())
639 }
640
641 #[cold]
643 #[inline(never)]
644 pub(crate) fn planner_invariant(message: impl Into<String>) -> Self {
645 Self::new(
646 ErrorClass::InvariantViolation,
647 ErrorOrigin::Planner,
648 message.into(),
649 )
650 }
651
652 #[must_use]
654 pub(crate) fn invalid_logical_plan_message(reason: impl Into<String>) -> String {
655 format!("invalid logical plan: {}", reason.into())
656 }
657
658 pub(crate) fn query_invalid_logical_plan(reason: impl Into<String>) -> Self {
660 Self::planner_invariant(Self::invalid_logical_plan_message(reason))
661 }
662
663 pub(crate) fn store_invariant(message: impl Into<String>) -> Self {
665 Self::new(
666 ErrorClass::InvariantViolation,
667 ErrorOrigin::Store,
668 message.into(),
669 )
670 }
671
672 pub(crate) fn duplicate_runtime_hooks_for_entity_tag(
674 entity_tag: crate::types::EntityTag,
675 ) -> Self {
676 Self::store_invariant(format!(
677 "duplicate runtime hooks for entity tag '{}'",
678 entity_tag.value()
679 ))
680 }
681
682 pub(crate) fn duplicate_runtime_hooks_for_entity_path(entity_path: &str) -> Self {
684 Self::store_invariant(format!(
685 "duplicate runtime hooks for entity path '{entity_path}'"
686 ))
687 }
688
689 #[cold]
691 #[inline(never)]
692 pub(crate) fn store_internal(message: impl Into<String>) -> Self {
693 Self::new(ErrorClass::Internal, ErrorOrigin::Store, message.into())
694 }
695
696 pub(crate) fn commit_memory_id_unconfigured() -> Self {
698 Self::store_internal(
699 "commit memory id is not configured; initialize recovery before commit store access",
700 )
701 }
702
703 pub(crate) fn commit_memory_id_mismatch(cached_id: u8, configured_id: u8) -> Self {
705 Self::store_internal(format!(
706 "commit memory id mismatch: cached={cached_id}, configured={configured_id}",
707 ))
708 }
709
710 pub(crate) fn commit_memory_stable_key_mismatch(
712 cached_key: &str,
713 configured_key: &str,
714 ) -> Self {
715 Self::store_internal(format!(
716 "commit memory stable key mismatch: cached={cached_key}, configured={configured_key}",
717 ))
718 }
719
720 pub(crate) fn delete_rollback_row_required() -> Self {
722 Self::store_internal("missing raw row for delete rollback")
723 }
724
725 pub(crate) fn recovery_integrity_validation_failed(
727 missing_index_entries: u64,
728 divergent_index_entries: u64,
729 orphan_index_references: u64,
730 ) -> Self {
731 Self::store_corruption(format!(
732 "recovery integrity validation failed: missing_index_entries={missing_index_entries} divergent_index_entries={divergent_index_entries} orphan_index_references={orphan_index_references}",
733 ))
734 }
735
736 #[cold]
738 #[inline(never)]
739 pub(crate) fn index_internal(message: impl Into<String>) -> Self {
740 Self::new(ErrorClass::Internal, ErrorOrigin::Index, message.into())
741 }
742
743 pub(crate) fn structural_index_removal_entity_key_required() -> Self {
745 Self::index_internal("missing old entity key for structural index removal")
746 }
747
748 pub(crate) fn structural_index_insertion_entity_key_required() -> Self {
750 Self::index_internal("missing new entity key for structural index insertion")
751 }
752
753 pub(crate) fn index_commit_op_old_entity_key_required() -> Self {
755 Self::index_internal("missing old entity key for index removal")
756 }
757
758 pub(crate) fn index_commit_op_new_entity_key_required() -> Self {
760 Self::index_internal("missing new entity key for index insertion")
761 }
762
763 #[cfg(test)]
765 pub(crate) fn query_internal(message: impl Into<String>) -> Self {
766 Self::new(ErrorClass::Internal, ErrorOrigin::Query, message.into())
767 }
768
769 #[cold]
771 #[inline(never)]
772 pub(crate) fn query_unsupported(message: impl Into<String>) -> Self {
773 Self::new(ErrorClass::Unsupported, ErrorOrigin::Query, message.into())
774 }
775
776 #[cold]
778 #[inline(never)]
779 #[cfg(feature = "sql")]
780 pub(crate) fn query_schema_ddl_admission(
781 error: SchemaDdlAdmissionError,
782 message: impl Into<String>,
783 ) -> Self {
784 Self {
785 class: ErrorClass::Unsupported,
786 origin: ErrorOrigin::Query,
787 message: message.into(),
788 detail: Some(ErrorDetail::Query(QueryErrorDetail::SchemaDdlAdmission {
789 error,
790 })),
791 }
792 }
793
794 #[cold]
796 #[inline(never)]
797 pub(crate) fn query_numeric_overflow() -> Self {
798 Self {
799 class: ErrorClass::Unsupported,
800 origin: ErrorOrigin::Query,
801 message: "numeric overflow".to_string(),
802 detail: Some(ErrorDetail::Query(QueryErrorDetail::NumericOverflow)),
803 }
804 }
805
806 #[cold]
809 #[inline(never)]
810 pub(crate) fn query_numeric_not_representable() -> Self {
811 Self {
812 class: ErrorClass::Unsupported,
813 origin: ErrorOrigin::Query,
814 message: "numeric result is not representable".to_string(),
815 detail: Some(ErrorDetail::Query(
816 QueryErrorDetail::NumericNotRepresentable,
817 )),
818 }
819 }
820
821 #[cold]
823 #[inline(never)]
824 pub(crate) fn serialize_internal(message: impl Into<String>) -> Self {
825 Self::new(ErrorClass::Internal, ErrorOrigin::Serialize, message.into())
826 }
827
828 pub(crate) fn persisted_row_encode_failed(detail: impl fmt::Display) -> Self {
830 Self::serialize_internal(format!("row encode failed: {detail}"))
831 }
832
833 pub(crate) fn persisted_row_field_encode_failed(
835 field_name: &str,
836 detail: impl fmt::Display,
837 ) -> Self {
838 Self::serialize_internal(format!(
839 "row encode failed for field '{field_name}': {detail}",
840 ))
841 }
842
843 pub(crate) fn bytes_field_value_encode_failed(detail: impl fmt::Display) -> Self {
845 Self::serialize_internal(format!("bytes(field) value encode failed: {detail}"))
846 }
847
848 #[cold]
850 #[inline(never)]
851 pub(crate) fn store_corruption(message: impl Into<String>) -> Self {
852 Self::new(ErrorClass::Corruption, ErrorOrigin::Store, message.into())
853 }
854
855 pub(crate) fn commit_corruption(detail: impl fmt::Display) -> Self {
857 Self::store_corruption(format!("commit marker corrupted: {detail}"))
858 }
859
860 pub(crate) fn commit_component_corruption(component: &str, detail: impl fmt::Display) -> Self {
862 Self::store_corruption(format!("commit marker {component} corrupted: {detail}"))
863 }
864
865 pub(crate) fn commit_id_generation_failed(detail: impl fmt::Display) -> Self {
867 Self::store_internal(format!("commit id generation failed: {detail}"))
868 }
869
870 pub(crate) fn commit_marker_payload_exceeds_u32_length_limit(label: &str, len: usize) -> Self {
872 Self::store_unsupported(format!("{label} exceeds u32 length limit: {len} bytes"))
873 }
874
875 pub(crate) fn commit_component_length_invalid(
877 component: &str,
878 len: usize,
879 expected: impl fmt::Display,
880 ) -> Self {
881 Self::commit_component_corruption(
882 component,
883 format!("invalid length {len}, expected {expected}"),
884 )
885 }
886
887 pub(crate) fn commit_marker_exceeds_max_size(size: usize, max_size: u32) -> Self {
889 Self::commit_corruption(format!(
890 "commit marker exceeds max size: {size} bytes (limit {max_size})",
891 ))
892 }
893
894 pub(crate) fn commit_control_slot_exceeds_max_size(size: usize, max_size: u32) -> Self {
896 Self::store_unsupported(format!(
897 "commit control slot exceeds max size: {size} bytes (limit {max_size})",
898 ))
899 }
900
901 pub(crate) fn commit_control_slot_marker_bytes_exceed_u32_length_limit(size: usize) -> Self {
903 Self::store_unsupported(format!(
904 "commit marker bytes exceed u32 length limit: {size} bytes",
905 ))
906 }
907
908 pub(crate) fn startup_index_rebuild_invalid_data_key(
910 store_path: &str,
911 detail: impl fmt::Display,
912 ) -> Self {
913 Self::store_corruption(format!(
914 "startup index rebuild failed: invalid data key in store '{store_path}' ({detail})",
915 ))
916 }
917
918 #[cold]
920 #[inline(never)]
921 pub(crate) fn index_corruption(message: impl Into<String>) -> Self {
922 Self::new(ErrorClass::Corruption, ErrorOrigin::Index, message.into())
923 }
924
925 pub(crate) fn index_unique_validation_corruption(
927 entity_path: &str,
928 fields: &str,
929 detail: impl fmt::Display,
930 ) -> Self {
931 Self::index_plan_index_corruption(format!(
932 "index corrupted: {entity_path} ({fields}) -> {detail}",
933 ))
934 }
935
936 pub(crate) fn structural_index_entry_corruption(
938 entity_path: &str,
939 fields: &str,
940 detail: impl fmt::Display,
941 ) -> Self {
942 Self::index_plan_index_corruption(format!(
943 "index corrupted: {entity_path} ({fields}) -> {detail}",
944 ))
945 }
946
947 pub(crate) fn index_unique_validation_entity_key_required() -> Self {
949 Self::index_invariant("missing entity key during unique validation")
950 }
951
952 pub(crate) fn index_unique_validation_row_deserialize_failed(
954 data_key: impl fmt::Display,
955 source: impl fmt::Display,
956 ) -> Self {
957 Self::index_plan_serialize_corruption(format!(
958 "failed to structurally deserialize row: {data_key} ({source})"
959 ))
960 }
961
962 pub(crate) fn index_unique_validation_primary_key_decode_failed(
964 data_key: impl fmt::Display,
965 source: impl fmt::Display,
966 ) -> Self {
967 Self::index_plan_serialize_corruption(format!(
968 "failed to decode structural primary-key slot: {data_key} ({source})"
969 ))
970 }
971
972 pub(crate) fn index_unique_validation_key_rebuild_failed(
974 data_key: impl fmt::Display,
975 entity_path: &str,
976 source: impl fmt::Display,
977 ) -> Self {
978 Self::index_plan_serialize_corruption(format!(
979 "failed to structurally decode unique key row {data_key} for {entity_path}: {source}",
980 ))
981 }
982
983 pub(crate) fn index_unique_validation_row_required(data_key: impl fmt::Display) -> Self {
985 Self::index_plan_store_corruption(format!("missing row: {data_key}"))
986 }
987
988 pub(crate) fn index_only_predicate_component_required() -> Self {
990 Self::index_invariant("index-only predicate program referenced missing index component")
991 }
992
993 pub(crate) fn index_scan_continuation_anchor_within_envelope_required() -> Self {
995 Self::index_invariant(
996 "index-range continuation anchor is outside the requested range envelope",
997 )
998 }
999
1000 pub(crate) fn index_scan_continuation_advancement_required() -> Self {
1002 Self::index_invariant("index-range continuation scan did not advance beyond the anchor")
1003 }
1004
1005 pub(crate) fn index_scan_key_corrupted_during(
1007 context: &'static str,
1008 err: impl fmt::Display,
1009 ) -> Self {
1010 Self::index_corruption(format!("index key corrupted during {context}: {err}"))
1011 }
1012
1013 pub(crate) fn index_projection_component_required(
1015 index_name: &str,
1016 component_index: usize,
1017 ) -> Self {
1018 Self::index_invariant(format!(
1019 "index projection referenced missing component: index='{index_name}' component_index={component_index}",
1020 ))
1021 }
1022
1023 pub(crate) fn index_entry_decode_failed(err: impl fmt::Display) -> Self {
1025 Self::index_corruption(err.to_string())
1026 }
1027
1028 pub(crate) fn serialize_corruption(message: impl Into<String>) -> Self {
1030 Self::new(
1031 ErrorClass::Corruption,
1032 ErrorOrigin::Serialize,
1033 message.into(),
1034 )
1035 }
1036
1037 pub(crate) fn persisted_row_decode_failed(detail: impl fmt::Display) -> Self {
1039 Self::serialize_corruption(format!("row decode: {detail}"))
1040 }
1041
1042 pub(crate) fn persisted_row_field_decode_failed(
1044 field_name: &str,
1045 detail: impl fmt::Display,
1046 ) -> Self {
1047 Self::serialize_corruption(format!(
1048 "row decode failed for field '{field_name}': {detail}",
1049 ))
1050 }
1051
1052 pub(crate) fn persisted_row_field_kind_decode_failed(
1054 field_name: &str,
1055 field_kind: impl fmt::Debug,
1056 detail: impl fmt::Display,
1057 ) -> Self {
1058 Self::persisted_row_field_decode_failed(
1059 field_name,
1060 format!("kind={field_kind:?}: {detail}"),
1061 )
1062 }
1063
1064 pub(crate) fn persisted_row_field_payload_exact_len_required(
1066 field_name: &str,
1067 payload_kind: &str,
1068 expected_len: usize,
1069 ) -> Self {
1070 let unit = if expected_len == 1 { "byte" } else { "bytes" };
1071
1072 Self::persisted_row_field_decode_failed(
1073 field_name,
1074 format!("{payload_kind} payload must be exactly {expected_len} {unit}"),
1075 )
1076 }
1077
1078 pub(crate) fn persisted_row_field_payload_must_be_empty(
1080 field_name: &str,
1081 payload_kind: &str,
1082 ) -> Self {
1083 Self::persisted_row_field_decode_failed(
1084 field_name,
1085 format!("{payload_kind} payload must be empty"),
1086 )
1087 }
1088
1089 pub(crate) fn persisted_row_field_payload_invalid_byte(
1091 field_name: &str,
1092 payload_kind: &str,
1093 value: u8,
1094 ) -> Self {
1095 Self::persisted_row_field_decode_failed(
1096 field_name,
1097 format!("invalid {payload_kind} payload byte {value}"),
1098 )
1099 }
1100
1101 pub(crate) fn persisted_row_field_payload_non_finite(
1103 field_name: &str,
1104 payload_kind: &str,
1105 ) -> Self {
1106 Self::persisted_row_field_decode_failed(
1107 field_name,
1108 format!("{payload_kind} payload is non-finite"),
1109 )
1110 }
1111
1112 pub(crate) fn persisted_row_field_payload_out_of_range(
1114 field_name: &str,
1115 payload_kind: &str,
1116 ) -> Self {
1117 Self::persisted_row_field_decode_failed(
1118 field_name,
1119 format!("{payload_kind} payload out of range for target type"),
1120 )
1121 }
1122
1123 pub(crate) fn persisted_row_field_text_payload_invalid_utf8(
1125 field_name: &str,
1126 detail: impl fmt::Display,
1127 ) -> Self {
1128 Self::persisted_row_field_decode_failed(
1129 field_name,
1130 format!("invalid UTF-8 text payload ({detail})"),
1131 )
1132 }
1133
1134 pub(crate) fn persisted_row_slot_lookup_out_of_bounds(model_path: &str, slot: usize) -> Self {
1136 Self::index_invariant(format!(
1137 "slot lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1138 ))
1139 }
1140
1141 pub(crate) fn persisted_row_slot_cache_lookup_out_of_bounds(
1143 model_path: &str,
1144 slot: usize,
1145 ) -> Self {
1146 Self::index_invariant(format!(
1147 "slot cache lookup outside model bounds during structural row access: model='{model_path}' slot={slot}",
1148 ))
1149 }
1150
1151 pub(crate) fn persisted_row_primary_key_not_primary_key_encodable(
1153 data_key: impl fmt::Debug,
1154 detail: impl fmt::Display,
1155 ) -> Self {
1156 Self::persisted_row_decode_failed(format!(
1157 "primary-key value is not primary-key encodable: {data_key:?} ({detail})",
1158 ))
1159 }
1160
1161 pub(crate) fn persisted_row_primary_key_slot_missing(data_key: impl fmt::Debug) -> Self {
1163 Self::persisted_row_decode_failed(format!(
1164 "missing primary-key slot while validating {data_key:?}",
1165 ))
1166 }
1167
1168 pub(crate) fn persisted_row_key_mismatch(
1170 expected_key: impl fmt::Debug,
1171 found_key: impl fmt::Debug,
1172 ) -> Self {
1173 Self::store_corruption(format!(
1174 "row key mismatch: expected {expected_key:?}, found {found_key:?}",
1175 ))
1176 }
1177
1178 pub(crate) fn persisted_row_declared_field_missing(field_name: &str) -> Self {
1180 Self::persisted_row_decode_failed(format!("missing declared field `{field_name}`"))
1181 }
1182
1183 pub(crate) fn data_key_entity_mismatch(
1185 expected: impl fmt::Display,
1186 found: impl fmt::Display,
1187 ) -> Self {
1188 Self::store_corruption(format!(
1189 "data key entity mismatch: expected {expected}, found {found}",
1190 ))
1191 }
1192
1193 pub(crate) fn reverse_index_ordinal_overflow(
1195 source_path: &str,
1196 field_name: &str,
1197 target_path: &str,
1198 detail: impl fmt::Display,
1199 ) -> Self {
1200 Self::index_internal(format!(
1201 "reverse index ordinal overflow: source={source_path} field={field_name} target={target_path} ({detail})",
1202 ))
1203 }
1204
1205 pub(crate) fn reverse_index_entry_corrupted(
1207 source_path: &str,
1208 field_name: &str,
1209 target_path: &str,
1210 index_key: impl fmt::Debug,
1211 detail: impl fmt::Display,
1212 ) -> Self {
1213 Self::index_corruption(format!(
1214 "reverse index entry corrupted: source={source_path} field={field_name} target={target_path} key={index_key:?} ({detail})",
1215 ))
1216 }
1217
1218 pub(crate) fn relation_target_store_missing(
1220 source_path: &str,
1221 field_name: &str,
1222 target_path: &str,
1223 store_path: &str,
1224 detail: impl fmt::Display,
1225 ) -> Self {
1226 Self::executor_internal(format!(
1227 "relation target store missing: source={source_path} field={field_name} target={target_path} store={store_path} ({detail})",
1228 ))
1229 }
1230
1231 pub(crate) fn relation_target_key_decode_failed(
1233 context_label: &str,
1234 source_path: &str,
1235 field_name: &str,
1236 target_path: &str,
1237 detail: impl fmt::Display,
1238 ) -> Self {
1239 Self::identity_corruption(format!(
1240 "{context_label}: source={source_path} field={field_name} target={target_path} ({detail})",
1241 ))
1242 }
1243
1244 pub(crate) fn relation_target_entity_mismatch(
1246 context_label: &str,
1247 source_path: &str,
1248 field_name: &str,
1249 target_path: &str,
1250 target_entity_name: &str,
1251 expected_tag: impl fmt::Display,
1252 actual_tag: impl fmt::Display,
1253 ) -> Self {
1254 Self::store_corruption(format!(
1255 "{context_label}: source={source_path} field={field_name} target={target_path} expected={target_entity_name} (tag={expected_tag}) actual_tag={actual_tag}",
1256 ))
1257 }
1258
1259 pub(crate) fn relation_source_row_decode_failed(
1261 source_path: &str,
1262 field_name: &str,
1263 target_path: &str,
1264 detail: impl fmt::Display,
1265 ) -> Self {
1266 Self::serialize_corruption(format!(
1267 "relation source row decode: source={source_path} field={field_name} target={target_path} ({detail})",
1268 ))
1269 }
1270
1271 pub(crate) fn relation_source_row_unsupported_scalar_relation_key(
1273 source_path: &str,
1274 field_name: &str,
1275 target_path: &str,
1276 ) -> Self {
1277 Self::serialize_corruption(format!(
1278 "relation source row decode: unsupported scalar relation key: source={source_path} field={field_name} target={target_path}",
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 schema_ddl_publication_race_lost(entity_path: &'static str) -> Self {
1371 let message = format!(
1372 "SQL DDL publication race lost for entity '{entity_path}': accepted schema changed after DDL binding",
1373 );
1374
1375 Self {
1376 class: ErrorClass::Unsupported,
1377 origin: ErrorOrigin::Store,
1378 message,
1379 detail: Some(ErrorDetail::Store(
1380 StoreError::SchemaDdlPublicationRaceLost {
1381 entity_path: entity_path.to_string(),
1382 },
1383 )),
1384 }
1385 }
1386
1387 pub(crate) fn unsupported_entity_tag_in_data_store(
1389 entity_tag: crate::types::EntityTag,
1390 ) -> Self {
1391 Self::store_unsupported(format!(
1392 "unsupported entity tag in data store: '{}'",
1393 entity_tag.value()
1394 ))
1395 }
1396
1397 #[cfg_attr(test, expect(dead_code))]
1399 pub(crate) fn commit_memory_id_registration_failed(err: impl fmt::Display) -> Self {
1400 Self::store_internal(format!("commit memory id registration failed: {err}"))
1401 }
1402
1403 pub(crate) fn index_unsupported(message: impl Into<String>) -> Self {
1405 Self::new(ErrorClass::Unsupported, ErrorOrigin::Index, message.into())
1406 }
1407
1408 pub(crate) fn index_component_exceeds_max_size(
1410 key_item: impl fmt::Display,
1411 len: usize,
1412 max_component_size: usize,
1413 ) -> Self {
1414 Self::index_unsupported(format!(
1415 "index component exceeds max size: key item '{key_item}' -> {len} bytes (limit {max_component_size})",
1416 ))
1417 }
1418
1419 pub(crate) fn serialize_unsupported(message: impl Into<String>) -> Self {
1421 Self::new(
1422 ErrorClass::Unsupported,
1423 ErrorOrigin::Serialize,
1424 message.into(),
1425 )
1426 }
1427
1428 pub(crate) fn cursor_unsupported(message: impl Into<String>) -> Self {
1430 Self::new(ErrorClass::Unsupported, ErrorOrigin::Cursor, message.into())
1431 }
1432
1433 pub(crate) fn serialize_incompatible_persisted_format(message: impl Into<String>) -> Self {
1435 Self::new(
1436 ErrorClass::IncompatiblePersistedFormat,
1437 ErrorOrigin::Serialize,
1438 message.into(),
1439 )
1440 }
1441
1442 #[cfg(feature = "sql")]
1445 pub(crate) fn query_unsupported_sql_feature(feature: diagnostic_code::SqlFeatureCode) -> Self {
1446 let message = format!(
1447 "SQL query is not executable in this release: unsupported SQL feature: {feature:?}"
1448 );
1449
1450 Self {
1451 class: ErrorClass::Unsupported,
1452 origin: ErrorOrigin::Query,
1453 message,
1454 detail: Some(ErrorDetail::Query(
1455 QueryErrorDetail::UnsupportedSqlFeature { feature },
1456 )),
1457 }
1458 }
1459
1460 #[cfg(feature = "sql")]
1463 pub(crate) fn query_sql_surface_mismatch(
1464 mismatch: diagnostic_code::SqlSurfaceMismatchCode,
1465 message: impl Into<String>,
1466 ) -> Self {
1467 Self {
1468 class: ErrorClass::Unsupported,
1469 origin: ErrorOrigin::Query,
1470 message: message.into(),
1471 detail: Some(ErrorDetail::Query(QueryErrorDetail::SqlSurfaceMismatch {
1472 mismatch,
1473 })),
1474 }
1475 }
1476
1477 pub fn store_not_found(key: impl Into<String>) -> Self {
1478 let key = key.into();
1479
1480 Self {
1481 class: ErrorClass::NotFound,
1482 origin: ErrorOrigin::Store,
1483 message: format!("data key not found: {key}"),
1484 detail: Some(ErrorDetail::Store(StoreError::NotFound { key })),
1485 }
1486 }
1487
1488 pub fn unsupported_entity_path(path: impl Into<String>) -> Self {
1490 let path = path.into();
1491
1492 Self::new(
1493 ErrorClass::Unsupported,
1494 ErrorOrigin::Store,
1495 format!("unsupported entity path: '{path}'"),
1496 )
1497 }
1498
1499 #[must_use]
1500 pub const fn is_not_found(&self) -> bool {
1501 matches!(
1502 self.detail,
1503 Some(ErrorDetail::Store(StoreError::NotFound { .. }))
1504 )
1505 }
1506
1507 #[must_use]
1508 pub fn display_with_class(&self) -> String {
1509 format!("{}:{}: {}", self.origin, self.class, self.message)
1510 }
1511
1512 #[cold]
1514 #[inline(never)]
1515 pub(crate) fn index_plan_corruption(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1516 let message = message.into();
1517 Self::new(
1518 ErrorClass::Corruption,
1519 origin,
1520 format!("corruption detected ({origin}): {message}"),
1521 )
1522 }
1523
1524 #[cold]
1526 #[inline(never)]
1527 pub(crate) fn index_plan_index_corruption(message: impl Into<String>) -> Self {
1528 Self::index_plan_corruption(ErrorOrigin::Index, message)
1529 }
1530
1531 #[cold]
1533 #[inline(never)]
1534 pub(crate) fn index_plan_store_corruption(message: impl Into<String>) -> Self {
1535 Self::index_plan_corruption(ErrorOrigin::Store, message)
1536 }
1537
1538 #[cold]
1540 #[inline(never)]
1541 pub(crate) fn index_plan_serialize_corruption(message: impl Into<String>) -> Self {
1542 Self::index_plan_corruption(ErrorOrigin::Serialize, message)
1543 }
1544
1545 #[cfg(test)]
1547 pub(crate) fn index_plan_invariant(origin: ErrorOrigin, message: impl Into<String>) -> Self {
1548 let message = message.into();
1549 Self::new(
1550 ErrorClass::InvariantViolation,
1551 origin,
1552 format!("invariant violation detected ({origin}): {message}"),
1553 )
1554 }
1555
1556 #[cfg(test)]
1558 pub(crate) fn index_plan_store_invariant(message: impl Into<String>) -> Self {
1559 Self::index_plan_invariant(ErrorOrigin::Store, message)
1560 }
1561
1562 pub(crate) fn index_violation(path: &str, index_fields: &[&str]) -> Self {
1564 Self::new(
1565 ErrorClass::Conflict,
1566 ErrorOrigin::Index,
1567 format!(
1568 "index constraint violation: {path} ({})",
1569 index_fields.join(", ")
1570 ),
1571 )
1572 }
1573}
1574
1575#[derive(Debug, ThisError)]
1583pub enum ErrorDetail {
1584 #[error("{0}")]
1585 Store(StoreError),
1586 #[error("{0}")]
1587 Query(QueryErrorDetail),
1588 }
1595
1596#[derive(Debug, ThisError)]
1604pub enum StoreError {
1605 #[error("key not found: {key}")]
1606 NotFound { key: String },
1607
1608 #[error("store corruption: {message}")]
1609 Corrupt { message: String },
1610
1611 #[error("store invariant violation: {message}")]
1612 InvariantViolation { message: String },
1613
1614 #[error("schema DDL publication race lost for entity: {entity_path}")]
1615 SchemaDdlPublicationRaceLost { entity_path: String },
1616}
1617
1618#[derive(Debug, ThisError)]
1625pub enum QueryErrorDetail {
1626 #[error("numeric overflow")]
1627 NumericOverflow,
1628
1629 #[error("numeric result is not representable")]
1630 NumericNotRepresentable,
1631
1632 #[error("unsupported SQL feature: {feature:?}")]
1633 UnsupportedSqlFeature {
1634 feature: diagnostic_code::SqlFeatureCode,
1635 },
1636
1637 #[error("SQL endpoint surface mismatch: {mismatch:?}")]
1638 SqlSurfaceMismatch {
1639 mismatch: diagnostic_code::SqlSurfaceMismatchCode,
1640 },
1641
1642 #[error("SQL DDL admission rejected: {error}")]
1643 SchemaDdlAdmission { error: SchemaDdlAdmissionError },
1644}
1645
1646#[derive(Clone, Copy, Debug, Eq, PartialEq, ThisError)]
1655pub enum SchemaDdlAdmissionError {
1656 #[error("missing expected schema version")]
1657 MissingExpectedSchemaVersion,
1658
1659 #[error("missing next schema version")]
1660 MissingNextSchemaVersion,
1661
1662 #[error("stale expected schema version")]
1663 StaleExpectedSchemaVersion,
1664
1665 #[error("invalid expected schema version")]
1666 InvalidExpectedSchemaVersion,
1667
1668 #[error("invalid next schema version")]
1669 InvalidNextSchemaVersion,
1670
1671 #[error("accepted schema changed without version bump")]
1672 AcceptedSchemaChangeWithoutVersionBump,
1673
1674 #[error("empty version bump")]
1675 EmptyVersionBump,
1676
1677 #[error("version gap")]
1678 VersionGap,
1679
1680 #[error("version rollback")]
1681 VersionRollback,
1682
1683 #[error("fingerprint method mismatch")]
1684 FingerprintMethodMismatch,
1685
1686 #[error("unsupported transition class")]
1687 UnsupportedTransitionClass,
1688
1689 #[error("physical runner missing")]
1690 PhysicalRunnerMissing,
1691
1692 #[error("validation failed")]
1693 ValidationFailed,
1694
1695 #[error("publication race lost")]
1696 PublicationRaceLost,
1697}
1698
1699impl ErrorDetail {
1700 #[must_use]
1702 pub const fn diagnostic_code(&self) -> diagnostic_code::DiagnosticCode {
1703 match self {
1704 Self::Store(error) => error.diagnostic_code(),
1705 Self::Query(error) => error.diagnostic_code(),
1706 }
1707 }
1708
1709 #[must_use]
1711 pub const fn diagnostic_detail(&self) -> Option<diagnostic_code::DiagnosticDetail> {
1712 match self {
1713 Self::Store(error) => error.diagnostic_detail(),
1714 Self::Query(error) => error.diagnostic_detail(),
1715 }
1716 }
1717}
1718
1719impl StoreError {
1720 #[must_use]
1722 pub const fn diagnostic_code(&self) -> diagnostic_code::DiagnosticCode {
1723 match self {
1724 Self::NotFound { .. } => diagnostic_code::DiagnosticCode::StoreNotFound,
1725 Self::Corrupt { .. } => diagnostic_code::DiagnosticCode::StoreCorruption,
1726 Self::InvariantViolation { .. } => {
1727 diagnostic_code::DiagnosticCode::StoreInvariantViolation
1728 }
1729 Self::SchemaDdlPublicationRaceLost { .. } => {
1730 diagnostic_code::DiagnosticCode::SchemaDdlAdmission
1731 }
1732 }
1733 }
1734
1735 #[must_use]
1737 pub const fn diagnostic_detail(&self) -> Option<diagnostic_code::DiagnosticDetail> {
1738 match self {
1739 Self::SchemaDdlPublicationRaceLost { .. } => {
1740 Some(diagnostic_code::DiagnosticDetail::SchemaDdlAdmission {
1741 reason: diagnostic_code::SchemaDdlAdmissionCode::PublicationRaceLost,
1742 })
1743 }
1744 Self::NotFound { .. } | Self::Corrupt { .. } | Self::InvariantViolation { .. } => None,
1745 }
1746 }
1747}
1748
1749impl QueryErrorDetail {
1750 #[must_use]
1752 pub const fn diagnostic_code(&self) -> diagnostic_code::DiagnosticCode {
1753 match self {
1754 Self::NumericOverflow => diagnostic_code::DiagnosticCode::QueryNumericOverflow,
1755 Self::NumericNotRepresentable => {
1756 diagnostic_code::DiagnosticCode::QueryNumericNotRepresentable
1757 }
1758 Self::UnsupportedSqlFeature { .. } => {
1759 diagnostic_code::DiagnosticCode::QueryUnsupportedSqlFeature
1760 }
1761 Self::SqlSurfaceMismatch { .. } => {
1762 diagnostic_code::DiagnosticCode::QuerySqlSurfaceMismatch
1763 }
1764 Self::SchemaDdlAdmission { .. } => diagnostic_code::DiagnosticCode::SchemaDdlAdmission,
1765 }
1766 }
1767
1768 #[must_use]
1770 pub const fn diagnostic_detail(&self) -> Option<diagnostic_code::DiagnosticDetail> {
1771 match self {
1772 Self::UnsupportedSqlFeature { feature } => {
1773 Some(diagnostic_code::DiagnosticDetail::UnsupportedSqlFeature { feature: *feature })
1774 }
1775 Self::SqlSurfaceMismatch { mismatch } => {
1776 Some(diagnostic_code::DiagnosticDetail::SqlSurfaceMismatch {
1777 mismatch: *mismatch,
1778 })
1779 }
1780 Self::SchemaDdlAdmission { error } => {
1781 Some(diagnostic_code::DiagnosticDetail::SchemaDdlAdmission {
1782 reason: error.diagnostic_code(),
1783 })
1784 }
1785 Self::NumericOverflow | Self::NumericNotRepresentable => None,
1786 }
1787 }
1788}
1789
1790impl SchemaDdlAdmissionError {
1791 #[must_use]
1793 pub const fn diagnostic_code(&self) -> diagnostic_code::SchemaDdlAdmissionCode {
1794 match self {
1795 Self::MissingExpectedSchemaVersion => {
1796 diagnostic_code::SchemaDdlAdmissionCode::MissingExpectedSchemaVersion
1797 }
1798 Self::MissingNextSchemaVersion => {
1799 diagnostic_code::SchemaDdlAdmissionCode::MissingNextSchemaVersion
1800 }
1801 Self::StaleExpectedSchemaVersion => {
1802 diagnostic_code::SchemaDdlAdmissionCode::StaleExpectedSchemaVersion
1803 }
1804 Self::InvalidExpectedSchemaVersion => {
1805 diagnostic_code::SchemaDdlAdmissionCode::InvalidExpectedSchemaVersion
1806 }
1807 Self::InvalidNextSchemaVersion => {
1808 diagnostic_code::SchemaDdlAdmissionCode::InvalidNextSchemaVersion
1809 }
1810 Self::AcceptedSchemaChangeWithoutVersionBump => {
1811 diagnostic_code::SchemaDdlAdmissionCode::AcceptedSchemaChangeWithoutVersionBump
1812 }
1813 Self::EmptyVersionBump => diagnostic_code::SchemaDdlAdmissionCode::EmptyVersionBump,
1814 Self::VersionGap => diagnostic_code::SchemaDdlAdmissionCode::VersionGap,
1815 Self::VersionRollback => diagnostic_code::SchemaDdlAdmissionCode::VersionRollback,
1816 Self::FingerprintMethodMismatch => {
1817 diagnostic_code::SchemaDdlAdmissionCode::FingerprintMethodMismatch
1818 }
1819 Self::UnsupportedTransitionClass => {
1820 diagnostic_code::SchemaDdlAdmissionCode::UnsupportedTransitionClass
1821 }
1822 Self::PhysicalRunnerMissing => {
1823 diagnostic_code::SchemaDdlAdmissionCode::PhysicalRunnerMissing
1824 }
1825 Self::ValidationFailed => diagnostic_code::SchemaDdlAdmissionCode::ValidationFailed,
1826 Self::PublicationRaceLost => {
1827 diagnostic_code::SchemaDdlAdmissionCode::PublicationRaceLost
1828 }
1829 }
1830 }
1831}
1832
1833#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1840pub enum ErrorClass {
1841 Corruption,
1842 IncompatiblePersistedFormat,
1843 NotFound,
1844 Internal,
1845 Conflict,
1846 Unsupported,
1847 InvariantViolation,
1848}
1849
1850impl ErrorClass {
1851 #[must_use]
1853 pub const fn diagnostic_code(self, origin: ErrorOrigin) -> diagnostic_code::DiagnosticCode {
1854 match self {
1855 Self::Corruption if matches!(origin, ErrorOrigin::Store) => {
1856 diagnostic_code::DiagnosticCode::StoreCorruption
1857 }
1858 Self::Corruption => diagnostic_code::DiagnosticCode::RuntimeCorruption,
1859 Self::IncompatiblePersistedFormat => {
1860 diagnostic_code::DiagnosticCode::RuntimeIncompatiblePersistedFormat
1861 }
1862 Self::NotFound if matches!(origin, ErrorOrigin::Store) => {
1863 diagnostic_code::DiagnosticCode::StoreNotFound
1864 }
1865 Self::NotFound => diagnostic_code::DiagnosticCode::RuntimeNotFound,
1866 Self::Internal => diagnostic_code::DiagnosticCode::RuntimeInternal,
1867 Self::Conflict => diagnostic_code::DiagnosticCode::RuntimeConflict,
1868 Self::Unsupported => diagnostic_code::DiagnosticCode::RuntimeUnsupported,
1869 Self::InvariantViolation if matches!(origin, ErrorOrigin::Store) => {
1870 diagnostic_code::DiagnosticCode::StoreInvariantViolation
1871 }
1872 Self::InvariantViolation => diagnostic_code::DiagnosticCode::RuntimeInvariantViolation,
1873 }
1874 }
1875}
1876
1877impl fmt::Display for ErrorClass {
1878 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1879 let label = match self {
1880 Self::Corruption => "corruption",
1881 Self::IncompatiblePersistedFormat => "incompatible_persisted_format",
1882 Self::NotFound => "not_found",
1883 Self::Internal => "internal",
1884 Self::Conflict => "conflict",
1885 Self::Unsupported => "unsupported",
1886 Self::InvariantViolation => "invariant_violation",
1887 };
1888 write!(f, "{label}")
1889 }
1890}
1891
1892#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1899pub enum ErrorOrigin {
1900 Serialize,
1901 Store,
1902 Index,
1903 Identity,
1904 Query,
1905 Planner,
1906 Cursor,
1907 Recovery,
1908 Response,
1909 Executor,
1910 Interface,
1911}
1912
1913impl ErrorOrigin {
1914 #[must_use]
1916 pub const fn diagnostic_origin(self) -> diagnostic_code::ErrorOrigin {
1917 match self {
1918 Self::Serialize => diagnostic_code::ErrorOrigin::Serialize,
1919 Self::Store => diagnostic_code::ErrorOrigin::Store,
1920 Self::Index => diagnostic_code::ErrorOrigin::Index,
1921 Self::Identity => diagnostic_code::ErrorOrigin::Identity,
1922 Self::Query => diagnostic_code::ErrorOrigin::Query,
1923 Self::Planner => diagnostic_code::ErrorOrigin::Planner,
1924 Self::Cursor => diagnostic_code::ErrorOrigin::Cursor,
1925 Self::Recovery => diagnostic_code::ErrorOrigin::Recovery,
1926 Self::Response => diagnostic_code::ErrorOrigin::Response,
1927 Self::Executor => diagnostic_code::ErrorOrigin::Executor,
1928 Self::Interface => diagnostic_code::ErrorOrigin::Interface,
1929 }
1930 }
1931}
1932
1933impl fmt::Display for ErrorOrigin {
1934 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1935 let label = match self {
1936 Self::Serialize => "serialize",
1937 Self::Store => "store",
1938 Self::Index => "index",
1939 Self::Identity => "identity",
1940 Self::Query => "query",
1941 Self::Planner => "planner",
1942 Self::Cursor => "cursor",
1943 Self::Recovery => "recovery",
1944 Self::Response => "response",
1945 Self::Executor => "executor",
1946 Self::Interface => "interface",
1947 };
1948 write!(f, "{label}")
1949 }
1950}