feat(error): add structured exit codes for scriptable error handling#428
Conversation
Replace the hardcoded `std::process::exit(1)` with a type-specific exit
code derived from the GwsError variant:
0 — success
1 — API error (GwsError::Api)
2 — auth error (GwsError::Auth)
3 — validation (GwsError::Validation)
4 — discovery (GwsError::Discovery)
5 — internal (GwsError::Other)
This allows shell scripts to branch on failure type without parsing the
JSON error output:
gws drive files list ...
case $? in
1) echo "API error — check your params" ;;
2) echo "Auth error — run: gws auth login" ;;
3) echo "Bad arguments" ;;
esac
Changes:
- Add GwsError::exit_code() mapping variants to codes
- Update main() to call std::process::exit(err.exit_code())
- Document exit codes in gws --help (print_usage)
- Document exit codes in README under new Exit Codes section
- Add 6 unit tests including a regression guard asserting all codes are distinct
🦋 Changeset detectedLatest commit: be31c31 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Summary of ChangesHello, 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 enhances the scriptability and robustness of the Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces structured exit codes, which is a great feature for scriptability. The implementation is straightforward and well-tested. I've added a few suggestions to src/error.rs to improve maintainability by replacing magic numbers with constants, which is important for a public contract like exit codes. The documentation changes in the README and help output are clear and helpful.
Add EXIT_CODE_API/AUTH/VALIDATION/DISCOVERY/OTHER associated constants on GwsError so callers and tests reference symbolic names rather than bare integers. Update exit_code() match arms and all tests accordingly. The distinctness test now validates the constants array directly. Addresses code-review feedback requesting named constants.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces structured exit codes, which is a great improvement for scriptability. The implementation is solid, with new constants for exit codes, an exit_code() method on GwsError, and accompanying tests. The documentation has also been updated accordingly. I have one suggestion to improve maintainability by reducing documentation duplication for the exit codes in the help text.
src/main.rs
Outdated
| println!("EXIT CODES:"); | ||
| println!(" 0 Success"); | ||
| println!(" 1 API error — Google returned an error response"); | ||
| println!(" 2 Auth error — credentials missing or invalid"); | ||
| println!(" 3 Validation — bad arguments or input"); | ||
| println!(" 4 Discovery — could not fetch API schema"); | ||
| println!(" 5 Internal — unexpected failure"); |
There was a problem hiding this comment.
The exit code documentation is hardcoded here using println!. This information is duplicated in several other places, including the doc comment in src/error.rs, README.md, and the changeset file. This duplication makes maintenance difficult and increases the risk of documentation becoming inconsistent with the implementation.
To improve maintainability and ensure a single source of truth for the help output, this information should be centralized. A good place would be src/error.rs alongside the exit code constants.
For example, you could add this constant to src/error.rs:
pub const EXIT_CODE_DOCUMENTATION: &[(i32, &str)] = &[
(0, "Success"),
(crate::error::GwsError::EXIT_CODE_API, "API error — Google returned an error response"),
(crate::error::GwsError::EXIT_CODE_AUTH, "Auth error — credentials missing or invalid"),
(crate::error::GwsError::EXIT_CODE_VALIDATION, "Validation — bad arguments or input"),
(crate::error::GwsError::EXIT_CODE_DISCOVERY, "Discovery — could not fetch API schema"),
(crate::error::GwsError::EXIT_CODE_OTHER, "Internal — unexpected failure"),
];And then replace this block of println! statements with a loop that uses the constant, ensuring the help text is always in sync with the code.
| println!("EXIT CODES:"); | |
| println!(" 0 Success"); | |
| println!(" 1 API error — Google returned an error response"); | |
| println!(" 2 Auth error — credentials missing or invalid"); | |
| println!(" 3 Validation — bad arguments or input"); | |
| println!(" 4 Discovery — could not fetch API schema"); | |
| println!(" 5 Internal — unexpected failure"); | |
| println!("EXIT CODES:"); | |
| for (code, description) in crate::error::EXIT_CODE_DOCUMENTATION { | |
| println!(" {:<5}{}", code, description); | |
| } |
…TATION Add a module-level EXIT_CODE_DOCUMENTATION constant — a static slice of (code, description) pairs built from the EXIT_CODE_* constants. Replace the hardcoded println! block in print_usage() with a loop over this slice so the help output is always in sync with the defined constants and cannot drift out of date. Addresses code-review feedback requesting a single source of truth.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces structured exit codes to the gws CLI tool, replacing the generic exit code 1 with specific codes for different error types (API, Auth, Validation, Discovery, and Internal errors). The changes include defining constant exit codes and a mapping function within the GwsError enum, updating the main application to use these new codes, and adding comprehensive documentation in the README.md, a new changeset file, and the gws --help output. Unit tests were also added to verify the correctness and distinctness of these exit codes.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #428 +/- ##
==========================================
+ Coverage 64.40% 64.47% +0.06%
==========================================
Files 38 38
Lines 15584 15633 +49
==========================================
+ Hits 10037 10079 +42
- Misses 5547 5554 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
std::process::exit(1)with a type-specific exit code derived from theGwsErrorvariantGwsError::exit_code()mapping each error kind to a stable, documented codegws --helpoutput and in the README under a new Exit Codes sectionExit code table
GwsErrorvariant01Api2Auth3Validation4Discovery5OtherUsage in scripts
Test plan
cargo test— 556 passed, 0 failedcargo clippy -- -D warnings— clean🤖 Generated with Claude Code