Skip to main content

libgrite_git/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur during Git operations
4#[derive(Debug, Error)]
5pub enum GitError {
6    #[error("Git error: {0}")]
7    Git(#[from] git2::Error),
8
9    #[error("IO error: {0}")]
10    Io(#[from] std::io::Error),
11
12    #[error("JSON error: {0}")]
13    Json(#[from] serde_json::Error),
14
15    #[error("CBOR decode error: {0}")]
16    CborDecode(String),
17
18    #[error("Invalid chunk format: {0}")]
19    InvalidChunk(String),
20
21    #[error("WAL error: {0}")]
22    Wal(String),
23
24    #[error("Snapshot error: {0}")]
25    Snapshot(String),
26
27    #[error("Sync error: {0}")]
28    Sync(String),
29
30    #[error("Invalid event data: {0}")]
31    InvalidEvent(String),
32
33    #[error("Not a git repository")]
34    NotARepo,
35
36    #[error("Parse error: {0}")]
37    ParseError(String),
38
39    #[error("Lock conflict: {resource} is locked by {owner} (expires in {expires_in_ms}ms)")]
40    LockConflict {
41        resource: String,
42        owner: String,
43        expires_in_ms: u64,
44    },
45
46    #[error("Lock not owned: {resource} is owned by {owner}")]
47    LockNotOwned { resource: String, owner: String },
48}
49
50/// Bridge GitError into GriteError, preserving semantic variants.
51impl From<GitError> for libgrite_core::GriteError {
52    fn from(e: GitError) -> Self {
53        match e {
54            GitError::LockConflict {
55                resource,
56                owner,
57                expires_in_ms,
58            } => libgrite_core::GriteError::Conflict(format!(
59                "Resource '{}' is locked by {} (expires in {}s)",
60                resource,
61                owner,
62                expires_in_ms / 1000
63            )),
64            GitError::LockNotOwned { resource, owner } => libgrite_core::GriteError::Conflict(
65                format!("Cannot release lock on '{}': owned by {}", resource, owner),
66            ),
67            GitError::NotARepo => {
68                libgrite_core::GriteError::NotFound("Not a git repository".to_string())
69            }
70            GitError::Git(g) if g.code() == git2::ErrorCode::NotFound => {
71                libgrite_core::GriteError::NotFound(g.message().to_string())
72            }
73            other => libgrite_core::GriteError::Internal(other.to_string()),
74        }
75    }
76}