Skip to main content

reifydb_cdc/
error.rs

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