Skip to main content

cooklang_sync_client/
errors.rs

1use 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("Can't parse the response")]
34    BodyExtractError,
35    #[error("Can't find in cache")]
36    GetFromCacheError,
37    #[error("Unlisted file format {0}")]
38    UnlistedFileFormat(String),
39    #[error("Unknown error: {0}")]
40    Unknown(String),
41    #[error("Batch download error {0}")]
42    BatchDownloadError(String),
43}
44
45impl SyncError {
46    pub fn from_io_error(path: impl Into<PathBuf>, error: std::io::Error) -> Self {
47        SyncError::IoError {
48            path: path.into().display().to_string(),
49            source: error,
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn io_error_from_conversion_preserves_message() {
60        let io = std::io::Error::new(std::io::ErrorKind::NotFound, "boom");
61        let err: SyncError = io.into();
62        let msg = format!("{err}");
63        assert!(msg.contains("boom"), "wrapped IO error message preserved: {msg}");
64        assert!(matches!(err, SyncError::IoErrorGeneric(_)));
65    }
66
67    #[test]
68    fn from_io_error_helper_attaches_path() {
69        let io = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "nope");
70        let err = SyncError::from_io_error("/some/path", io);
71        let msg = format!("{err}");
72        assert!(msg.contains("/some/path"), "path should be in message: {msg}");
73        assert!(msg.contains("nope"), "source cause should be in message: {msg}");
74        assert!(matches!(err, SyncError::IoError { .. }));
75    }
76
77    #[test]
78    fn unauthorized_has_stable_display() {
79        let err = SyncError::Unauthorized;
80        assert_eq!(format!("{err}"), "Unauthorized token");
81    }
82
83    #[test]
84    fn unknown_variant_includes_context() {
85        let err = SyncError::Unknown("xyz".into());
86        assert!(format!("{err}").contains("xyz"));
87    }
88}