cooklang_sync_client/
errors.rs1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5#[cfg_attr(feature = "ffi", derive(uniffi::Error))]
6#[cfg_attr(feature = "ffi", uniffi(flat_error))]
7pub enum SyncError {
8 #[error("IO error in file {path}: {source}")]
9 IoError {
10 path: String,
11 source: std::io::Error,
12 },
13 #[error("IO error {0}")]
14 IoErrorGeneric(#[from] std::io::Error),
15 #[error("Notify error {0}")]
16 NotifyError(#[from] notify::Error),
17 #[error("Strip prefix error {0}")]
18 StripPrefix(#[from] std::path::StripPrefixError),
19 #[error("System time error {0}")]
20 SystemTime(#[from] std::time::SystemTimeError),
21 #[error("Conversion error {0}")]
22 Convert(#[from] std::num::TryFromIntError),
23 #[error("Database query error {0}")]
24 DBQueryError(#[from] diesel::result::Error),
25 #[error("Reqwest error {0}")]
26 ReqwestError(#[from] reqwest::Error),
27 #[error("Error sending value to a channel {0}")]
28 ChannelSendError(#[from] futures::channel::mpsc::SendError),
29 #[error("Connection init error {0}")]
30 ConnectionInitError(String),
31 #[error("Unauthorized token")]
32 Unauthorized,
33 #[error("Sync requires a paid plan")]
34 PaymentRequired,
35 #[error("Can't parse the response")]
36 BodyExtractError,
37 #[error("Can't find in cache")]
38 GetFromCacheError,
39 #[error("Unlisted file format {0}")]
40 UnlistedFileFormat(String),
41 #[error("Unknown error: {0}")]
42 Unknown(String),
43 #[error("Batch download error {0}")]
44 BatchDownloadError(String),
45}
46
47impl SyncError {
48 pub fn from_io_error(path: impl Into<PathBuf>, error: std::io::Error) -> Self {
49 SyncError::IoError {
50 path: path.into().display().to_string(),
51 source: error,
52 }
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn io_error_from_conversion_preserves_message() {
62 let io = std::io::Error::new(std::io::ErrorKind::NotFound, "boom");
63 let err: SyncError = io.into();
64 let msg = format!("{err}");
65 assert!(
66 msg.contains("boom"),
67 "wrapped IO error message preserved: {msg}"
68 );
69 assert!(matches!(err, SyncError::IoErrorGeneric(_)));
70 }
71
72 #[test]
73 fn from_io_error_helper_attaches_path() {
74 let io = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "nope");
75 let err = SyncError::from_io_error("/some/path", io);
76 let msg = format!("{err}");
77 assert!(
78 msg.contains("/some/path"),
79 "path should be in message: {msg}"
80 );
81 assert!(
82 msg.contains("nope"),
83 "source cause should be in message: {msg}"
84 );
85 assert!(matches!(err, SyncError::IoError { .. }));
86 }
87
88 #[test]
89 fn unauthorized_has_stable_display() {
90 let err = SyncError::Unauthorized;
91 assert_eq!(format!("{err}"), "Unauthorized token");
92 }
93
94 #[test]
95 fn payment_required_has_stable_display() {
96 let err = SyncError::PaymentRequired;
97 assert_eq!(format!("{err}"), "Sync requires a paid plan");
98 }
99
100 #[test]
101 fn unknown_variant_includes_context() {
102 let err = SyncError::Unknown("xyz".into());
103 assert!(format!("{err}").contains("xyz"));
104 }
105}