1#[cfg(test)]
2mod tests;
3
4#[cfg(test)]
5use crate::db::query::plan::{PlanError, PolicyPlanError};
6use crate::{
7 db::{access::AccessPlanError, cursor::CursorPlanError},
8 patch::MergePatchError,
9};
10use std::fmt;
11use thiserror::Error as ThisError;
12
13#[derive(Debug, ThisError)]
114#[error("{message}")]
115pub struct InternalError {
116 pub class: ErrorClass,
117 pub origin: ErrorOrigin,
118 pub message: String,
119
120 pub detail: Option<ErrorDetail>,
123}
124
125impl InternalError {
126 pub fn new(class: ErrorClass, origin: ErrorOrigin, message: impl Into<String>) -> Self {
130 let message = message.into();
131
132 let detail = match (class, origin) {
133 (ErrorClass::Corruption, ErrorOrigin::Store) => {
134 Some(ErrorDetail::Store(StoreError::Corrupt {
135 message: message.clone(),
136 }))
137 }
138 (ErrorClass::InvariantViolation, ErrorOrigin::Store) => {
139 Some(ErrorDetail::Store(StoreError::InvariantViolation {
140 message: message.clone(),
141 }))
142 }
143 _ => None,
144 };
145
146 Self {
147 class,
148 origin,
149 message,
150 detail,
151 }
152 }
153
154 pub(crate) fn classified(
156 class: ErrorClass,
157 origin: ErrorOrigin,
158 message: impl Into<String>,
159 ) -> Self {
160 Self::new(class, origin, message)
161 }
162
163 pub(crate) fn with_message(self, message: impl Into<String>) -> Self {
165 Self::classified(self.class, self.origin, message)
166 }
167
168 pub(crate) fn with_origin(self, origin: ErrorOrigin) -> Self {
172 Self::classified(self.class, origin, self.message)
173 }
174
175 pub(crate) fn query_invariant(message: impl Into<String>) -> Self {
177 Self::new(
178 ErrorClass::InvariantViolation,
179 ErrorOrigin::Query,
180 message.into(),
181 )
182 }
183
184 pub(crate) fn planner_invariant(message: impl Into<String>) -> Self {
186 Self::new(
187 ErrorClass::InvariantViolation,
188 ErrorOrigin::Planner,
189 message.into(),
190 )
191 }
192
193 #[must_use]
195 pub(crate) fn executor_invariant_message(reason: impl Into<String>) -> String {
196 format!("executor invariant violated: {}", reason.into())
197 }
198
199 #[must_use]
201 pub(crate) fn invalid_logical_plan_message(reason: impl Into<String>) -> String {
202 format!("invalid logical plan: {}", reason.into())
203 }
204
205 pub(crate) fn query_executor_invariant(reason: impl Into<String>) -> Self {
207 Self::query_invariant(Self::executor_invariant_message(reason))
208 }
209
210 pub(crate) fn query_invalid_logical_plan(reason: impl Into<String>) -> Self {
212 Self::planner_invariant(Self::invalid_logical_plan_message(reason))
213 }
214
215 pub(crate) fn cursor_invariant(message: impl Into<String>) -> Self {
217 Self::new(
218 ErrorClass::InvariantViolation,
219 ErrorOrigin::Cursor,
220 message.into(),
221 )
222 }
223
224 pub(crate) fn index_invariant(message: impl Into<String>) -> Self {
226 Self::new(
227 ErrorClass::InvariantViolation,
228 ErrorOrigin::Index,
229 message.into(),
230 )
231 }
232
233 pub(crate) fn executor_invariant(message: impl Into<String>) -> Self {
235 Self::new(
236 ErrorClass::InvariantViolation,
237 ErrorOrigin::Executor,
238 message.into(),
239 )
240 }
241
242 pub(crate) fn store_invariant(message: impl Into<String>) -> Self {
244 Self::new(
245 ErrorClass::InvariantViolation,
246 ErrorOrigin::Store,
247 message.into(),
248 )
249 }
250
251 pub(crate) fn store_internal(message: impl Into<String>) -> Self {
253 Self::new(ErrorClass::Internal, ErrorOrigin::Store, message.into())
254 }
255
256 pub(crate) fn executor_internal(message: impl Into<String>) -> Self {
258 Self::new(ErrorClass::Internal, ErrorOrigin::Executor, message.into())
259 }
260
261 pub(crate) fn index_internal(message: impl Into<String>) -> Self {
263 Self::new(ErrorClass::Internal, ErrorOrigin::Index, message.into())
264 }
265
266 #[cfg(test)]
268 pub(crate) fn query_internal(message: impl Into<String>) -> Self {
269 Self::new(ErrorClass::Internal, ErrorOrigin::Query, message.into())
270 }
271
272 pub(crate) fn serialize_internal(message: impl Into<String>) -> Self {
274 Self::new(ErrorClass::Internal, ErrorOrigin::Serialize, message.into())
275 }
276
277 pub(crate) fn store_corruption(message: impl Into<String>) -> Self {
279 Self::new(ErrorClass::Corruption, ErrorOrigin::Store, message.into())
280 }
281
282 pub(crate) fn index_corruption(message: impl Into<String>) -> Self {
284 Self::new(ErrorClass::Corruption, ErrorOrigin::Index, message.into())
285 }
286
287 pub(crate) fn serialize_corruption(message: impl Into<String>) -> Self {
289 Self::new(
290 ErrorClass::Corruption,
291 ErrorOrigin::Serialize,
292 message.into(),
293 )
294 }
295
296 pub(crate) fn identity_corruption(message: impl Into<String>) -> Self {
298 Self::new(
299 ErrorClass::Corruption,
300 ErrorOrigin::Identity,
301 message.into(),
302 )
303 }
304
305 pub(crate) fn store_unsupported(message: impl Into<String>) -> Self {
307 Self::new(ErrorClass::Unsupported, ErrorOrigin::Store, message.into())
308 }
309
310 pub(crate) fn index_unsupported(message: impl Into<String>) -> Self {
312 Self::new(ErrorClass::Unsupported, ErrorOrigin::Index, message.into())
313 }
314
315 pub(crate) fn executor_unsupported(message: impl Into<String>) -> Self {
317 Self::new(
318 ErrorClass::Unsupported,
319 ErrorOrigin::Executor,
320 message.into(),
321 )
322 }
323
324 pub(crate) fn serialize_unsupported(message: impl Into<String>) -> Self {
326 Self::new(
327 ErrorClass::Unsupported,
328 ErrorOrigin::Serialize,
329 message.into(),
330 )
331 }
332
333 pub(crate) fn cursor_unsupported(message: impl Into<String>) -> Self {
335 Self::new(ErrorClass::Unsupported, ErrorOrigin::Cursor, message.into())
336 }
337
338 pub fn store_not_found(key: impl Into<String>) -> Self {
339 let key = key.into();
340
341 Self {
342 class: ErrorClass::NotFound,
343 origin: ErrorOrigin::Store,
344 message: format!("data key not found: {key}"),
345 detail: Some(ErrorDetail::Store(StoreError::NotFound { key })),
346 }
347 }
348
349 pub fn unsupported_entity_path(path: impl Into<String>) -> Self {
351 let path = path.into();
352
353 Self::new(
354 ErrorClass::Unsupported,
355 ErrorOrigin::Store,
356 format!("unsupported entity path: '{path}'"),
357 )
358 }
359
360 #[must_use]
361 pub const fn is_not_found(&self) -> bool {
362 matches!(
363 self.detail,
364 Some(ErrorDetail::Store(StoreError::NotFound { .. }))
365 )
366 }
367
368 #[must_use]
369 pub fn display_with_class(&self) -> String {
370 format!("{}:{}: {}", self.origin, self.class, self.message)
371 }
372
373 pub(crate) fn index_plan_corruption(origin: ErrorOrigin, message: impl Into<String>) -> Self {
375 let message = message.into();
376 Self::new(
377 ErrorClass::Corruption,
378 origin,
379 format!("corruption detected ({origin}): {message}"),
380 )
381 }
382
383 pub(crate) fn index_plan_index_corruption(message: impl Into<String>) -> Self {
385 Self::index_plan_corruption(ErrorOrigin::Index, message)
386 }
387
388 pub(crate) fn index_plan_store_corruption(message: impl Into<String>) -> Self {
390 Self::index_plan_corruption(ErrorOrigin::Store, message)
391 }
392
393 pub(crate) fn index_plan_serialize_corruption(message: impl Into<String>) -> Self {
395 Self::index_plan_corruption(ErrorOrigin::Serialize, message)
396 }
397
398 pub(crate) fn index_plan_invariant(origin: ErrorOrigin, message: impl Into<String>) -> Self {
400 let message = message.into();
401 Self::new(
402 ErrorClass::InvariantViolation,
403 origin,
404 format!("invariant violation detected ({origin}): {message}"),
405 )
406 }
407
408 pub(crate) fn index_plan_store_invariant(message: impl Into<String>) -> Self {
410 Self::index_plan_invariant(ErrorOrigin::Store, message)
411 }
412
413 pub(crate) fn index_violation(path: &str, index_fields: &[&str]) -> Self {
415 Self::new(
416 ErrorClass::Conflict,
417 ErrorOrigin::Index,
418 format!(
419 "index constraint violation: {path} ({})",
420 index_fields.join(", ")
421 ),
422 )
423 }
424
425 pub(crate) fn from_cursor_plan_error(err: CursorPlanError) -> Self {
431 match err {
432 CursorPlanError::ContinuationCursorInvariantViolation { reason } => {
433 Self::cursor_invariant(reason)
434 }
435 CursorPlanError::InvalidContinuationCursor { .. }
436 | CursorPlanError::InvalidContinuationCursorPayload { .. }
437 | CursorPlanError::ContinuationCursorVersionMismatch { .. }
438 | CursorPlanError::ContinuationCursorSignatureMismatch { .. }
439 | CursorPlanError::ContinuationCursorBoundaryArityMismatch { .. }
440 | CursorPlanError::ContinuationCursorWindowMismatch { .. }
441 | CursorPlanError::ContinuationCursorBoundaryTypeMismatch { .. }
442 | CursorPlanError::ContinuationCursorPrimaryKeyTypeMismatch { .. } => {
443 Self::cursor_unsupported(err.to_string())
444 }
445 }
446 }
447
448 #[cfg(test)]
450 pub(crate) fn from_group_plan_error(err: PlanError) -> Self {
451 let message = match &err {
452 PlanError::User(inner) => match inner.as_ref() {
453 crate::db::query::plan::PlanUserError::Group(inner) => {
454 format!("invalid logical plan: {inner}")
455 }
456 _ => err.to_string(),
457 },
458 PlanError::Policy(inner) => match inner.as_ref() {
459 crate::db::query::plan::PlanPolicyError::Group(inner) => {
460 format!("invalid logical plan: {inner}")
461 }
462 crate::db::query::plan::PlanPolicyError::Policy(_) => err.to_string(),
463 },
464 PlanError::Cursor(_) => err.to_string(),
465 };
466
467 Self::planner_invariant(message)
468 }
469
470 pub(crate) fn from_executor_access_plan_error(err: AccessPlanError) -> Self {
472 Self::query_invariant(err.to_string())
473 }
474
475 #[cfg(test)]
478 pub(crate) fn plan_invariant_violation(err: PolicyPlanError) -> Self {
479 let reason = match err {
480 PolicyPlanError::EmptyOrderSpec => {
481 "order specification must include at least one field"
482 }
483 PolicyPlanError::DeletePlanWithPagination => "delete plans must not include pagination",
484 PolicyPlanError::LoadPlanWithDeleteLimit => "load plans must not carry delete limits",
485 PolicyPlanError::DeleteLimitRequiresOrder => "delete limit requires explicit ordering",
486 PolicyPlanError::UnorderedPagination => "pagination requires explicit ordering",
487 };
488
489 Self::planner_invariant(Self::executor_invariant_message(reason))
490 }
491}
492
493#[derive(Debug, ThisError)]
501pub enum ErrorDetail {
502 #[error("{0}")]
503 Store(StoreError),
504 #[error("{0}")]
505 ViewPatch(crate::patch::MergePatchError),
506 }
516
517impl From<MergePatchError> for InternalError {
518 fn from(err: MergePatchError) -> Self {
519 Self {
520 class: ErrorClass::Unsupported,
521 origin: ErrorOrigin::Interface,
522 message: err.to_string(),
523 detail: Some(ErrorDetail::ViewPatch(err)),
524 }
525 }
526}
527
528#[derive(Debug, ThisError)]
536pub enum StoreError {
537 #[error("key not found: {key}")]
538 NotFound { key: String },
539
540 #[error("store corruption: {message}")]
541 Corrupt { message: String },
542
543 #[error("store invariant violation: {message}")]
544 InvariantViolation { message: String },
545}
546
547#[derive(Clone, Copy, Debug, Eq, PartialEq)]
554pub enum ErrorClass {
555 Corruption,
556 NotFound,
557 Internal,
558 Conflict,
559 Unsupported,
560 InvariantViolation,
561}
562
563impl fmt::Display for ErrorClass {
564 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
565 let label = match self {
566 Self::Corruption => "corruption",
567 Self::NotFound => "not_found",
568 Self::Internal => "internal",
569 Self::Conflict => "conflict",
570 Self::Unsupported => "unsupported",
571 Self::InvariantViolation => "invariant_violation",
572 };
573 write!(f, "{label}")
574 }
575}
576
577#[derive(Clone, Copy, Debug, Eq, PartialEq)]
584pub enum ErrorOrigin {
585 Serialize,
586 Store,
587 Index,
588 Identity,
589 Query,
590 Planner,
591 Cursor,
592 Recovery,
593 Response,
594 Executor,
595 Interface,
596}
597
598impl fmt::Display for ErrorOrigin {
599 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
600 let label = match self {
601 Self::Serialize => "serialize",
602 Self::Store => "store",
603 Self::Index => "index",
604 Self::Identity => "identity",
605 Self::Query => "query",
606 Self::Planner => "planner",
607 Self::Cursor => "cursor",
608 Self::Recovery => "recovery",
609 Self::Response => "response",
610 Self::Executor => "executor",
611 Self::Interface => "interface",
612 };
613 write!(f, "{label}")
614 }
615}