use serde_json::{Value, json};
pub fn emit(command: &str, code: &str, message: &str, context: Value) {
let mut document = json!({
"command": command,
"errors": [{"code": code, "message": message}],
});
if let (Some(object), Value::Object(extra)) = (document.as_object_mut(), context) {
for (key, value) in extra {
object.insert(key, value);
}
}
println!(
"{}",
serde_json::to_string_pretty(&document).expect("JSON document is serializable")
);
}
pub fn code_for(error: &anyhow::Error, fallback: &'static str) -> &'static str {
for cause in error.chain() {
if let Some(core) = cause.downcast_ref::<oxo_flow_core::error::OxoFlowError>() {
return match core {
oxo_flow_core::error::OxoFlowError::Parse { .. }
| oxo_flow_core::error::OxoFlowError::Toml(_)
| oxo_flow_core::error::OxoFlowError::Config { .. } => "PARSE",
_ => fallback,
};
}
}
fallback
}