Skip to main content

fallow_cli/
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.
5pub fn emit_error(message: &str, exit_code: u8, output: fallow_config::OutputFormat) -> ExitCode {
6    if matches!(output, fallow_config::OutputFormat::Json) {
7        let error_obj = serde_json::json!({
8            "error": true,
9            "message": message,
10            "exit_code": exit_code,
11        });
12        if let Ok(json) = serde_json::to_string_pretty(&error_obj) {
13            println!("{json}");
14        }
15    } else {
16        eprintln!("Error: {message}");
17    }
18    ExitCode::from(exit_code)
19}