Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-serialization-error-exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Exit non-zero with a clear error message when JSON serialization fails instead of printing empty stdout
23 changes: 21 additions & 2 deletions crates/google-workspace-cli/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ impl OutputFormat {
/// Format a JSON value according to the specified output format.
pub fn format_value(value: &Value, format: &OutputFormat) -> String {
match format {
OutputFormat::Json => serde_json::to_string_pretty(value).unwrap_or_default(),
OutputFormat::Json => match serde_json::to_string_pretty(value) {
Ok(s) => s,
Err(e) => {
eprintln!("error: failed to serialize response to JSON: {e}");
std::process::exit(1);
}
},
Comment thread
nuthalapativarun marked this conversation as resolved.
Outdated
OutputFormat::Table => format_table(value),
OutputFormat::Yaml => format_yaml(value),
OutputFormat::Csv => format_csv(value),
Expand All @@ -78,7 +84,13 @@ pub fn format_value(value: &Value, format: &OutputFormat) -> String {
/// combined stream is a valid YAML multi-document file.
pub fn format_value_paginated(value: &Value, format: &OutputFormat, is_first_page: bool) -> String {
match format {
OutputFormat::Json => serde_json::to_string(value).unwrap_or_default(),
OutputFormat::Json => match serde_json::to_string(value) {
Ok(s) => s,
Err(e) => {
eprintln!("error: failed to serialize response to JSON: {e}");
std::process::exit(1);
}
},
Comment thread
nuthalapativarun marked this conversation as resolved.
Outdated
OutputFormat::Csv => format_csv_page(value, is_first_page),
OutputFormat::Table => format_table_page(value, is_first_page),
// Prefix every page with a YAML document separator so that the
Expand Down Expand Up @@ -474,6 +486,13 @@ mod tests {
assert!(output.contains("\"test\""));
}

#[test]
fn test_format_value_json_non_empty() {
let val = json!({"key": "value"});
let result = format_value(&val, &OutputFormat::Json);
assert!(!result.is_empty());
}

#[test]
fn test_format_table_array_of_objects() {
let val = json!({
Expand Down
Loading