Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Step 2: Handle Errors

Construct deps_core::DepsError directly at call sites instead of using a local error wrapper. Use deps_core::Result<T> for function signatures:

#![allow(unused)]
fn main() {
use deps_core::{DepsError, InvalidPackageName};

/// Example: validation function — use `InvalidPackageName` for a malformed package/module
/// identifier, not `InvalidVersionReq` (reserved for a malformed version-requirement string).
fn validate_module_path(path: &str) -> Result<(), InvalidPackageName> {
    if path.is_empty() {
        return Err(InvalidPackageName::new("module path is empty"));
    }
    if path.contains("..") {
        return Err(InvalidPackageName::new(format!("invalid module path: {path}")));
    }
    Ok(())
}

/// Example: parsing function
fn parse_manifest(content: &str, uri: &url::Url) -> deps_core::Result<ParseResult> {
    // Parse logic...
    // On error: return Err(DepsError::ParseError { ... })
    // On success: return Ok(ParseResult { ... })
}

/// Example: registry function handling 404
const REGISTRY: &str = "example-registry";

fn fetch_versions(package: &str) -> deps_core::Result<Vec<Version>> {
    let response = http_client.get(&url).send()
        .map_err(|e| DepsError::CacheError(e.to_string()))?;
    
    if response.status() == 404 {
        return Err(DepsError::PackageNotFound {
            package: package.into(),
            registry: REGISTRY,
        });
    }
    
    let data: Vec<Version> = response.json()
        .map_err(|e| DepsError::ApiResponse {
            package: package.into(),
            registry: REGISTRY,
            source: e,
        })?;
    
    Ok(data)
}
}