Skip to main content

reifydb_cdc/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::{error::Error as StdError, fmt, fmt::Display};
5
6use reifydb_core::common::CommitVersion;
7use reifydb_type::{error, error::Error};
8
9/// Error type for CDC operations.
10#[derive(Debug, Clone)]
11pub enum CdcError {
12	/// The operation failed due to an internal error.
13	Internal(String),
14	/// The CDC entry was not found.
15	NotFound(CommitVersion),
16	/// Encoding or decoding failed.
17	Codec(String),
18}
19
20impl Display for CdcError {
21	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22		match self {
23			CdcError::Internal(msg) => write!(f, "CDC storage internal error: {}", msg),
24			CdcError::NotFound(version) => write!(f, "CDC entry not found: {:?}", version),
25			CdcError::Codec(msg) => write!(f, "CDC codec error: {}", msg),
26		}
27	}
28}
29
30impl StdError for CdcError {}
31
32impl From<CdcError> for Error {
33	fn from(err: CdcError) -> Self {
34		error!(match err {
35			CdcError::Internal(msg) => diagnostic::storage_error(msg),
36			CdcError::NotFound(version) => diagnostic::not_found(version.0),
37			CdcError::Codec(msg) => diagnostic::codec_error(msg),
38		})
39	}
40}
41
42/// Result type for CDC operations.
43pub type CdcResult<T> = Result<T, CdcError>;
44
45/// CDC-specific diagnostics.
46pub mod diagnostic {
47
48	use reifydb_type::{error::Diagnostic, fragment::Fragment};
49
50	/// CDC storage operation failed
51	pub fn storage_error(msg: impl Into<String>) -> Diagnostic {
52		Diagnostic {
53			code: "CDC_001".to_string(),
54			rql: None,
55			message: format!("CDC storage error: {}", msg.into()),
56			column: None,
57			fragment: Fragment::None,
58			label: None,
59			help: Some("Check CDC storage configuration and availability".to_string()),
60			notes: vec![],
61			cause: None,
62			operator_chain: None,
63		}
64	}
65
66	/// CDC entry not found at the specified version
67	pub fn not_found(version: u64) -> Diagnostic {
68		Diagnostic {
69			code: "CDC_002".to_string(),
70			rql: None,
71			message: format!("CDC entry not found for version {}", version),
72			column: None,
73			fragment: Fragment::None,
74			label: None,
75			help: Some("The requested CDC version may have been garbage collected or never existed"
76				.to_string()),
77			notes: vec![],
78			cause: None,
79			operator_chain: None,
80		}
81	}
82
83	/// CDC encoding/decoding failed
84	pub fn codec_error(msg: impl Into<String>) -> Diagnostic {
85		Diagnostic {
86			code: "CDC_003".to_string(),
87			rql: None,
88			message: format!("CDC codec error: {}", msg.into()),
89			column: None,
90			fragment: Fragment::None,
91			label: None,
92			help: Some("This may indicate data corruption or version mismatch".to_string()),
93			notes: vec![],
94			cause: None,
95			operator_chain: None,
96		}
97	}
98}