Skip to main content

fallow_engine/
error.rs

1use std::process::ExitCode;
2
3/// Emit an error as structured JSON on stdout when `--format json` is active,
4/// then return the given exit code. For non-JSON formats, emit to stderr as usual.
5#[expect(
6    clippy::print_stdout,
7    clippy::print_stderr,
8    reason = "structured error emission for CLI surfaces, preserved verbatim from the CLI error module"
9)]
10pub fn emit_error(message: &str, exit_code: u8, output: fallow_config::OutputFormat) -> ExitCode {
11    if matches!(output, fallow_config::OutputFormat::Json) {
12        let error_obj = serde_json::json!({
13            "error": true,
14            "message": message,
15            "exit_code": exit_code,
16        });
17        if let Ok(json) = serde_json::to_string_pretty(&error_obj) {
18            println!("{json}");
19        }
20    } else {
21        eprintln!("Error: {message}");
22    }
23    ExitCode::from(exit_code)
24}