Skip to main content

vtcode_core/tools/editing/patch/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5/// Errors produced while parsing or applying agent patches.
6#[derive(Debug, Error)]
7pub enum PatchError {
8    #[error("cannot parse empty patch input")]
9    EmptyInput,
10
11    #[error("patch does not contain any operations")]
12    NoOperations,
13
14    #[error("invalid patch format: {0}")]
15    InvalidFormat(String),
16
17    #[error("invalid patch hunk on line {line}: {message}")]
18    InvalidHunk { line: usize, message: String },
19
20    #[error("invalid patch operation for '{path}': {reason}")]
21    InvalidOperation { path: String, reason: String },
22
23    #[error("invalid path for {operation}: {path} ({reason})")]
24    InvalidPath {
25        operation: &'static str,
26        path: String,
27        reason: String,
28    },
29
30    #[error("file '{path}' not found for update")]
31    MissingFile { path: String },
32
33    #[error(
34        "failed to locate context '{context}' in '{path}'. Fix: Use read_file to get exact content, then copy the context lines precisely."
35    )]
36    ContextNotFound { path: String, context: String },
37
38    #[error(
39        "failed to locate expected lines in '{path}':\n{snippet}\n\nFix: The patch context/deletion lines must match file content exactly. Use read_file first, then use those exact lines in your patch."
40    )]
41    SegmentNotFound { path: String, snippet: String },
42
43    #[error(
44        "semantic patch anchor '{anchor}' for '{path}' could not be resolved safely: {reason}\n\nFix: Use a more specific @@ anchor such as a function/class name, or copy exact context lines from read_file."
45    )]
46    SemanticResolutionFailed {
47        path: String,
48        anchor: String,
49        reason: String,
50    },
51
52    #[error(
53        "semantic patch anchor '{anchor}' for '{path}' matched {candidate_count} possible locations. Use a more specific @@ anchor or smaller hunk."
54    )]
55    SemanticAmbiguous {
56        path: String,
57        anchor: String,
58        candidate_count: usize,
59    },
60
61    #[error("I/O error while {action} '{path}': {source}")]
62    Io {
63        action: &'static str,
64        path: PathBuf,
65        #[source]
66        source: std::io::Error,
67    },
68
69    #[error("failed to generate temporary path for '{path}': {source}")]
70    TempPath {
71        path: PathBuf,
72        #[source]
73        source: std::time::SystemTimeError,
74    },
75
76    #[error("failed to rollback patch after error ({original}): {rollback}")]
77    Recovery {
78        original: Box<PatchError>,
79        #[source]
80        rollback: Box<PatchError>,
81    },
82
83    #[error("{original}")]
84    RolledBack {
85        #[source]
86        original: Box<PatchError>,
87    },
88}