Skip to content

fix(sheets): report error for invalid --json-values instead of silent empty append#463

Open
anshul-garg27 wants to merge 2 commits intogoogleworkspace:mainfrom
anshul-garg27:fix/sheets-json-parse-error
Open

fix(sheets): report error for invalid --json-values instead of silent empty append#463
anshul-garg27 wants to merge 2 commits intogoogleworkspace:mainfrom
anshul-garg27:fix/sheets-json-parse-error

Conversation

@anshul-garg27
Copy link
Contributor

Summary

  • +append --json-values "bad input" silently appended an empty row because unwrap_or_default() discarded the parse error
  • Now returns a clear validation error: --json-values is not valid JSON: <details>. Expected a JSON array like '["a","b"]' or '[["a","b"],["c","d"]]'.

Changes

  • parse_append_args() returns Result<AppendConfig, GwsError> instead of bare AppendConfig
  • Propagate the ? at the call site in handle()
  • Add #[derive(Debug)] to AppendConfig (required for Result::unwrap() in tests)
  • Add test_parse_append_args_json_valid and test_parse_append_args_json_invalid_returns_error

Test plan

  • cargo test — 586 passed, 0 failed
  • cargo clippy -- -D warnings — clean

Generated with Claude Code

@googleworkspace-bot googleworkspace-bot added the area: core Core CLI parsing, commands, error handling, utilities label Mar 13, 2026
@changeset-bot
Copy link

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: d3d3a51

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness and user experience of the +append command by addressing a critical flaw where invalid JSON input for --json-values would lead to silent failures. By implementing explicit error handling and propagating detailed validation messages, the changes ensure that users are immediately informed of malformed input, thereby preventing data integrity issues and making the tool more reliable.

Highlights

  • Improved Error Reporting: The +append command now reports clear validation errors for invalid --json-values input, preventing silent failures and empty row appends.
  • Enhanced Error Propagation: The parse_append_args function has been refactored to return a Result type, allowing for proper error handling and propagation throughout the call stack.
  • Debugging Support: The AppendConfig struct now derives the Debug trait, which aids in debugging by providing a printable representation of its instances.
  • Comprehensive Testing: New unit tests have been added to specifically validate the parsing of both valid and invalid JSON inputs for the +append command, ensuring the new error handling works as expected.
Changelog
  • src/helpers/sheets.rs
    • Modified the handle function to correctly propagate errors returned by parse_append_args using the ? operator.
    • Added #[derive(Debug)] to the AppendConfig struct to enable debug printing.
    • Updated the parse_append_args function to return Result<AppendConfig, GwsError> and to generate a GwsError::Validation with a descriptive message when --json-values contains invalid JSON.
    • Introduced test_parse_append_args_json_valid and test_parse_append_args_json_invalid_returns_error to verify the new JSON parsing and error handling logic.
Activity
  • The pull request includes a detailed summary of changes and a test plan.
  • All cargo test runs passed with 586 tests and 0 failures.
  • cargo clippy -- -D warnings ran clean, indicating no new warnings were introduced.
  • The PR description notes that it was generated with Claude Code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses an issue where invalid JSON for --json-values was silently ignored. The change to parse_append_args to return a Result and propagate the error is a good solution. I've included one suggestion to further improve the JSON parsing logic by using a serde untagged enum, which would make it more efficient and maintainable.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses the silent failure when parsing invalid JSON for --json-values by returning a validation error. The changes to propagate the error and the addition of tests are well-implemented. However, I've identified a potential issue in the new parsing logic that could lead to silent data loss in cases of mixed-type JSON arrays. My suggestion aims to make the parsing stricter and more robust to prevent this.

@anshul-garg27
Copy link
Contributor Author

Ready for review — all tests passing, Gemini feedback addressed. @jpoehnelt

@jpoehnelt
Copy link
Member

need to resolve conflicts

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 82.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.28%. Comparing base (e3e1e86) to head (ca1a241).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/helpers/sheets.rs 82.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #463      +/-   ##
==========================================
+ Coverage   67.22%   67.28%   +0.05%     
==========================================
  Files          38       38              
  Lines       16679    16721      +42     
==========================================
+ Hits        11213    11250      +37     
- Misses       5466     5471       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

… empty append

parse_append_args() used unwrap_or_default() when JSON parsing failed,
silently converting invalid input into an empty Vec. This caused
+append --json-values with malformed JSON to append an empty row
without any error message, making debugging very difficult.

- Change parse_append_args() to return Result<AppendConfig, GwsError>
- Surface the serde_json error message with guidance on expected format
- Add #[derive(Debug)] to AppendConfig for Result unwrap in tests
- Add tests for valid JSON and invalid JSON error paths
@anshul-garg27 anshul-garg27 force-pushed the fix/sheets-json-parse-error branch from ca1a241 to d13d206 Compare March 13, 2026 20:17
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses a silent failure where invalid JSON for --json-values was ignored. The change to propagate a Result and return a validation error is a significant improvement. The new tests effectively cover the error case. I've added one suggestion to further enhance the error reporting by distinguishing between JSON syntax errors and incorrect data structures, which will provide more precise feedback to the user.

Comment on lines 280 to 284
} else {
eprintln!(
"Warning: --json-values is not valid JSON; expected an array or array-of-arrays"
);
Vec::new()
return Err(GwsError::Validation(format!(
"--json-values is not valid JSON: expected an array like '[\"a\",\"b\"]' or array-of-arrays like '[[\"a\",\"b\"],[\"c\",\"d\"]]'."
)));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current error message for invalid --json-values can be misleading. It reports "not valid JSON" even for inputs that are syntactically correct JSON but have the wrong structure (e.g., an object instead of an array). To provide more accurate feedback, it's better to first attempt to parse the input as a generic serde_json::Value. This allows you to distinguish between a syntax error and a structure/type error, and include the specific parsing error from serde_json in the message, as alluded to by the <details> in the pull request description.

        } else {
            // Check if it's a JSON syntax error or just a type mismatch.
            let err_msg = if let Err(e) = serde_json::from_str::<serde_json::Value>(json_str) {
                format!("--json-values is not valid JSON: {e}")
            } else {
                "--json-values has an incorrect structure".to_string()
            };
            return Err(GwsError::Validation(format!(
                "{err_msg}. Expected an array like '[\"a\",\"b\"]' or array-of-arrays like '[[\"a\",\"b\"],[\"c\",\"d\"]]'."
            )));
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, updated the error to first check if it's valid JSON at all vs valid JSON but wrong structure, so the message is more accurate

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses an issue where invalid JSON for --json-values failed silently. The change to return a validation error is a significant improvement. My review includes a suggestion to make the new error message more detailed by including the specific parsing error, which would further improve usability. I also noticed a duplicated test case that should be removed to maintain a clean test suite.

Comment on lines +281 to +283
return Err(GwsError::Validation(format!(
"--json-values is not valid JSON: expected an array like '[\"a\",\"b\"]' or array-of-arrays like '[[\"a\",\"b\"],[\"c\",\"d\"]]'."
)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The PR description mentions including error details (<details>) for invalid JSON, but the current implementation provides a generic error message. To improve the user experience and align with the PR's goal, consider including the specific parsing error from serde_json. This will help users debug their input more effectively.

Suggested change
return Err(GwsError::Validation(format!(
"--json-values is not valid JSON: expected an array like '[\"a\",\"b\"]' or array-of-arrays like '[[\"a\",\"b\"],[\"c\",\"d\"]]'."
)));
let err = serde_json::from_str::<serde_json::Value>(json_str).unwrap_err();
let msg = format!("--json-values is not valid JSON: {err}. Expected a JSON array like '[\"a\",\"b\"]' or array-of-arrays like '[[\"a\",\"b\"],[\"c\",\"d\"]]'.");
return Err(GwsError::Validation(msg));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, added the serde_json error details to the message so users can see exactly what went wrong

Comment on lines +451 to +462
#[test]
fn test_parse_append_args_json_valid() {
let matches = make_matches_append(&[
"test",
"--spreadsheet",
"123",
"--json-values",
r#"["a","b","c"]"#,
]);
let config = parse_append_args(&matches).unwrap();
assert_eq!(config.values, vec![vec!["a", "b", "c"]]);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test, test_parse_append_args_json_valid, is a duplicate of the existing test_parse_append_args_json_single_row test (lines 404-415). To keep the test suite clean and maintainable, this redundant test should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, removed the duplicate test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Core CLI parsing, commands, error handling, utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants