Expand description
Error handling for AGPM
This module provides comprehensive error types and user-friendly error reporting for the AGPM package manager. The error system is designed around two core principles:
- Strongly-typed errors for precise error handling in code
- User-friendly messages with actionable suggestions for CLI users
§Architecture
The error system consists of two main types:
AgpmError- Enumerated error types for all failure cases in AGPMErrorContext- Wrapper that adds user-friendly messages and suggestions
§Error Categories
AGPM errors are organized into several categories:
- Git Operations:
AgpmError::GitNotFound,AgpmError::GitCommandError, etc. - File System:
AgpmError::FileSystemError,AgpmError::PermissionDenied, etc. - Configuration:
AgpmError::ManifestNotFound,AgpmError::ManifestParseError, etc. - Dependencies:
AgpmError::CircularDependency,AgpmError::DependencyNotMet, etc. - Resources:
AgpmError::ResourceNotFound,AgpmError::InvalidResource, etc.
§Error Conversion and Context
Common standard library errors are automatically converted to AGPM errors:
std::io::Error→AgpmError::IoErrortoml::de::Error→AgpmError::TomlErrorsemver::Error→AgpmError::SemverError
Convert errors into user-friendly format with contextual suggestions.
§Examples
§Basic Error Handling
use agpm_cli::core::{AgpmError, ErrorContext};
fn handle_git_operation() -> Result<(), AgpmError> {
// Simulate a git operation failure
Err(AgpmError::GitNotFound)
}
match handle_git_operation() {
Ok(_) => println!("Success!"),
Err(e) => {
let ctx = ErrorContext::new(e)
.with_suggestion("Install git from https://git-scm.com/")
.with_details("AGPM requires git for repository operations");
ctx.display(); // Shows colored error with suggestions
}
}§Creating Error Context Manually
use agpm_cli::core::{AgpmError, ErrorContext};
let error = AgpmError::ManifestNotFound;
let context = ErrorContext::new(error)
.with_suggestion("Create a agpm.toml file in your project directory")
.with_details("AGPM searches for agpm.toml in current and parent directories");
// Display with colors in terminal
context.display();
// Or get as string for logging
let message = format!("{}", context);§Error Recovery Patterns
use agpm_cli::core::{AgpmError, ErrorContext};
fn install_dependency(name: &str) -> Result<(), AgpmError> {
// Try installation
match perform_installation(name) {
Ok(()) => Ok(()),
Err(e) => {
// Convert to user-friendly error for CLI display
let friendly = ErrorContext::new(e)
.with_suggestion(format!("Check the dependency name '{}' and try again", name))
.with_details("AGPM will attempt to install the dependency and its requirements");
friendly.display(); // Shows colored error with suggestions
Err(AgpmError::Other { message: "Installation failed".to_string() })
}
}
}
fn perform_installation(_name: &str) -> Result<(), AgpmError> {
// Implementation would go here
Ok(())
}Structs§
- Error
Context - Error context wrapper that provides user-friendly error information
Enums§
- Agpm
Error - The main error type for AGPM operations
Traits§
- Into
Anyhow With Context - Extension trait for converting
AgpmErrortoanyhow::Errorwith context