Skip to main content

git_vet/
error.rs

1use std::fmt;
2
3use thiserror::Error;
4
5use crate::channel::ChannelTransferError;
6use crate::path::{PathError, RepoPath, RepoPathScope};
7use crate::remote::RemoteError;
8
9#[derive(Debug, Error)]
10pub enum AppError {
11    #[error("git operation failed while {operation}: {details}")]
12    Git {
13        operation: &'static str,
14        details: String,
15    },
16    #[error("repository has no worktree")]
17    MissingWorktree,
18    #[error("path is not valid UTF-8")]
19    NonUtf8Path,
20    #[error("path escapes the repository root")]
21    PathOutsideRepo,
22    #[error("path must be absolute")]
23    NonAbsolutePath,
24    #[error("empty paths are not valid tracked files")]
25    EmptyPath,
26    #[error("repo path is invalid: {details}")]
27    InvalidRepoPath { details: &'static str },
28    #[error("path is not tracked at HEAD: {0}")]
29    PathNotTracked(RepoPath),
30    #[error("pathspec did not match any tracked files at HEAD: {0}")]
31    PathspecNotMatched(RepoPathScope),
32    #[error("path is a submodule/gitlink and is out of scope: {0}")]
33    PathIsSubmodule(RepoPath),
34    #[error("failed to read vetignore file: {0}")]
35    Vetignore(String),
36    #[error("missing git config user.name")]
37    MissingUserName,
38    #[error("missing git config user.email")]
39    MissingUserEmail,
40    #[error("git config {key} is not valid UTF-8: {details}")]
41    NonUtf8GitConfig { key: &'static str, details: String },
42    #[error(
43        "target paths have uncommitted working-tree changes; rerun with --allow-dirty to proceed with committed HEAD contents"
44    )]
45    DirtyPathsRequireAllowDirty,
46    #[error("aborted because target paths have uncommitted working-tree changes")]
47    DirtyPathsDeclined,
48    #[error("invalid review channel {channel:?}: {details}")]
49    InvalidChannel { channel: String, details: String },
50    #[error("source and destination channels must differ")]
51    SameChannelTransfer,
52    #[error("source channel {channel:?} has no local review notes")]
53    MissingSourceChannelNotes { channel: String },
54    #[error("destination channel {channel:?} already has local review notes")]
55    ExistingDestinationChannelNotes { channel: String },
56    #[error("source channel {channel:?} has a symbolic notes ref; expected a direct ref")]
57    SymbolicChannelNotesRef { channel: String },
58    #[error("--channel cannot be used with `{command}`; pass SOURCE and DESTINATION explicitly")]
59    ChannelOptionNotAllowed { command: &'static str },
60    #[error("sync remote error: {0}")]
61    Remote(#[from] RemoteError),
62    #[error("I/O error: {0}")]
63    Io(#[from] std::io::Error),
64    #[error("JSON error: {0}")]
65    Json(#[from] serde_json::Error),
66}
67
68impl From<ChannelTransferError> for AppError {
69    fn from(error: ChannelTransferError) -> Self {
70        match error {
71            ChannelTransferError::SameChannel => Self::SameChannelTransfer,
72        }
73    }
74}
75
76impl From<PathError> for AppError {
77    fn from(error: PathError) -> Self {
78        match error {
79            PathError::NonUtf8 => Self::NonUtf8Path,
80            PathError::OutsideRepo => Self::PathOutsideRepo,
81            PathError::NonAbsolute => Self::NonAbsolutePath,
82            PathError::EmptyPath => Self::EmptyPath,
83            PathError::EmptyComponent => Self::InvalidRepoPath {
84                details: "path contains an empty component",
85            },
86            PathError::CurrentDirComponent => Self::InvalidRepoPath {
87                details: "path contains a current-directory component",
88            },
89            PathError::NulByte => Self::InvalidRepoPath {
90                details: "path contains a NUL byte",
91            },
92        }
93    }
94}
95
96pub(crate) fn git_error(operation: &'static str, source: impl fmt::Display) -> AppError {
97    AppError::Git {
98        operation,
99        details: source.to_string(),
100    }
101}