Skip to main content

fallow_cli/
error.rs

1use std::process::ExitCode;
2
3use fallow_config::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"
11)]
12pub fn emit_error(message: &str, exit_code: u8, output: OutputFormat) -> ExitCode {
13    if matches!(output, OutputFormat::Json) {
14        let error_obj = fallow_output::ErrorOutput::new(message, exit_code);
15        if let Ok(json) = serde_json::to_string_pretty(&error_obj) {
16            println!("{json}");
17        }
18    } else {
19        eprintln!("Error: {message}");
20    }
21    ExitCode::from(exit_code)
22}