camel_cli/commands/errors.rs
1//! Centralized CLI error helpers.
2//!
3//! Provides a single point for emitting CLI-subcommand failure logs +
4//! exiting with the appropriate status code. Used by the `run` subcommand
5//! (and future subcommands) to keep error-handling consistent.
6//!
7//! Per ADR-0012, CLI bootstrap/subcommand failures are category (d) — keep
8//! `error!` with `// log-policy: system-broken` annotation. The annotation
9//! lives at each call site in the subcommand bodies.
10
11use camel_api::CamelError;
12
13/// Report a fatal CLI subcommand failure: print to stderr and exit(1).
14///
15/// Category (d) per ADR-0012 — the caller's `error!` invocation MUST be
16/// preceded by `// log-policy: system-broken`. This helper exists to
17/// deduplicate the `eprintln!` + `std::process::exit(1)` pattern, NOT to
18/// replace the `error!` log (which is the ADR-mandated operator signal).
19///
20/// Use when the failure is a CLI-bootstrap problem (config read, route
21/// discovery, context start) that should terminate the process with exit
22/// code 1.
23pub fn report_cli_failure_and_exit(ctx: &str, e: &CamelError) -> ! {
24 eprintln!("camel-cli {ctx} failed: {e}");
25 std::process::exit(1);
26}
27
28/// Report a fatal CLI subcommand failure with a non-CamelError error type.
29///
30/// Same semantics as [`report_cli_failure_and_exit`], for use when the
31/// underlying error is not a `CamelError` (e.g., `std::io::Error` from
32/// signal handling, `anyhow::Error` from a dependency).
33// Reserved for future non-CamelError failure paths (e.g., signal-handler io::Error); unused in Phase C Task 2.
34#[allow(dead_code)]
35pub fn report_cli_failure_msg_and_exit(ctx: &str, msg: impl std::fmt::Display) -> ! {
36 eprintln!("camel-cli {ctx} failed: {msg}");
37 std::process::exit(1);
38}