Skip to main content

oversync_core/
error.rs

1#[derive(Debug, thiserror::Error)]
2pub enum OversyncError {
3	#[error("surrealdb: {0}")]
4	SurrealDb(String),
5
6	#[error("connector: {0}")]
7	Connector(String),
8
9	#[error("sink: {0}")]
10	Sink(String),
11
12	#[error("config: {0}")]
13	Config(String),
14
15	#[error("migration: {0}")]
16	Migration(String),
17
18	#[error("plugin: {0}")]
19	Plugin(String),
20
21	#[error("internal: {0}")]
22	Internal(String),
23}
24
25impl From<serde_json::Error> for OversyncError {
26	fn from(e: serde_json::Error) -> Self {
27		Self::Internal(e.to_string())
28	}
29}
30
31#[cfg(test)]
32mod tests {
33	use super::*;
34
35	#[test]
36	fn display_all_variants() {
37		let cases: Vec<(OversyncError, &str)> = vec![
38			(OversyncError::SurrealDb("conn".into()), "surrealdb: conn"),
39			(
40				OversyncError::Connector("timeout".into()),
41				"connector: timeout",
42			),
43			(OversyncError::Sink("full".into()), "sink: full"),
44			(OversyncError::Config("bad".into()), "config: bad"),
45			(OversyncError::Migration("v1".into()), "migration: v1"),
46			(OversyncError::Plugin("missing".into()), "plugin: missing"),
47			(OversyncError::Internal("oops".into()), "internal: oops"),
48		];
49		for (err, expected) in cases {
50			assert_eq!(err.to_string(), expected);
51		}
52	}
53
54	#[test]
55	fn serde_json_error_converts() {
56		let bad_json = serde_json::from_str::<serde_json::Value>("not json");
57		let oversync_err: OversyncError = bad_json.unwrap_err().into();
58		assert!(matches!(oversync_err, OversyncError::Internal(_)));
59	}
60}