clawless_cli/error.rs
1//! Error types and result aliases for Clawless commands
2//!
3//! This module re-exports [`anyhow`] error handling primitives and defines the [`CommandResult`]
4//! type alias used as the standard return type for command functions.
5
6/// Trait for adding context to errors
7///
8/// This is a re-export of `anyhow::Context` that provides the `.context()` method
9/// for adding contextual information to errors. It's renamed to avoid conflicts
10/// with a future `clawless::Context` type for application state.
11///
12/// # Example
13///
14/// ```rust,ignore
15/// use clawless::ErrorContext;
16///
17/// let result = some_operation()
18/// .context("Failed to perform operation")?;
19/// ```
20pub use anyhow::Context as ErrorContext;
21pub use anyhow::Error;
22
23/// Result type for Clawless commands
24///
25/// Commands in Clawless execute a piece of logic that might fail for various
26/// reasons. If such an error is unrecoverable during the execution of the
27/// command, it will cause the CLI to fail and exit with an error message.
28///
29/// To make it easier to handle errors when implementing commands, every command
30/// handler returns a `CommandResult` type. This makes it possible to use the
31/// question mark `?` operator and return early when an unrecoverable error
32/// occurs.
33///
34/// The `CommandResult` is a type alias for [`anyhow::Result<T>`], which provides
35/// a more ergonomic way to handle arbitrary errors. Since it isn't possible to
36/// recover from the error, we do not need to provide a specific error type
37/// that a caller could handle gracefully.
38///
39/// The success type defaults to `()`. A command that returns `Ok(())` ends the
40/// process with the exit code 0, and a command that returns an error ends it
41/// with the exit code 1 and the text of the error. A command that chooses its
42/// exit code and its text returns an [`Exit`] instead, as `CommandResult<Exit>`.
43///
44/// # Examples
45///
46/// ```
47/// use std::process::ExitCode;
48///
49/// use clawless_cli::error::CommandResult;
50/// use clawless_core::exit::Exit;
51///
52/// fn check(major_version: u64) -> CommandResult<Exit> {
53/// if major_version == 0 {
54/// return Ok(Exit::builder()
55/// .code(ExitCode::from(3))
56/// .text("error: the version is not a stable release\n")
57/// .build());
58/// }
59///
60/// Ok(Exit::from(()))
61/// }
62/// ```
63///
64/// [`Exit`]: clawless_core::exit::Exit
65/// [`anyhow::Result<T>`]: anyhow::Result
66pub type CommandResult<T = ()> = anyhow::Result<T>;