vtcode_core/tools/
error_helpers.rs1use anyhow::{Context, Result};
4use std::fmt::Display;
5use std::path::Path;
6
7pub const PATCH_PARAMETER_HINT: &str = "(use 'input' or 'patch' parameter)";
13
14pub const USE_PATCH_FOR_LARGE_EDITS: &str = "For larger edits, use apply_patch instead.";
16
17pub const USE_PATCH_OR_WRITE_FOR_LARGE_FILES: &str = "For large files, use apply_patch or a dedicated write operation.";
19
20pub const READ_FILE_FIRST_HINT: &str = "Use read_file first to get the exact text, then copy it precisely.";
22
23pub fn with_file_context<T, E>(result: std::result::Result<T, E>, operation: impl Display, path: &Path) -> Result<T>
26where
27 E: std::error::Error + Send + Sync + 'static,
28{
29 result.with_context(|| format!("Failed to {operation} '{}'", path.display()))
30}
31
32pub fn with_path_context<T, E>(
34 result: std::result::Result<T, E>,
35 operation: impl Display,
36 path: impl Display,
37) -> Result<T>
38where
39 E: std::error::Error + Send + Sync + 'static,
40{
41 result.with_context(|| format!("Failed to {operation} {path}"))
42}
43
44#[cold]
46pub fn require_string_field(args: &serde_json::Value, field: &str, tool_name: &str) -> Result<String> {
47 args.get(field)
48 .and_then(|v| v.as_str())
49 .map(|s| s.to_string())
50 .ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
51}
52
53pub fn optional_string_field(args: &serde_json::Value, field: &str) -> Option<String> {
55 args.get(field).and_then(|v| v.as_str()).map(|s| s.to_string())
56}
57
58#[cold]
60pub fn require_int_field(args: &serde_json::Value, field: &str, tool_name: &str) -> Result<i64> {
61 args.get(field)
62 .and_then(|v| v.as_i64())
63 .ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
64}
65
66#[cold]
69pub fn require_field<T>(value: Option<T>, field: &str, tool_name: &str) -> Result<T> {
70 value.ok_or_else(|| anyhow::anyhow!("{field} is required for {tool_name}"))
71}
72
73pub fn deserialize_tool_args<T: serde::de::DeserializeOwned>(args: &serde_json::Value, tool_name: &str) -> Result<T> {
78 serde_json::from_value(args.clone()).map_err(|e| anyhow::anyhow!("Error: Invalid '{tool_name}' arguments: {e}"))
79}