Skip to main content

fallow_engine/
error.rs

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