-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: use exitCodes enum everywhere #1570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,9 +180,9 @@ catch (error: any) { } | |
|
|
||
| Commands use a small set of exit codes aligned with oclif defaults and Unix convention. | ||
|
|
||
| - **0 - Success**: Command completed normally. Implicit when `run()` returns without throwing. Only use `this.exit(0)` when you need to short-circuit early on a successful path. | ||
| - **1 - Runtime error**: Something went wrong during execution that is not the user's fault. API failures, network errors, missing project config, file system errors, unexpected state. Use `this.output.error(message, {exit: 1})`. | ||
| - **2 - Usage error**: The user provided invalid input to the CLI itself. Bad arguments, unknown flags, invalid flag values, failing input validation. This is oclif's default for `this.output.error()` and all parse errors, so omitting the `exit` option also gives you 2. Use `this.output.error(message, {exit: 2})` or `this.output.error(message)`. | ||
| - **0 - Success**: Command completed normally. Implicit when `run()` returns without throwing. Only use `this.exit(exitCodes.SUCCESS)` when you need to short-circuit early on a successful path. | ||
| - **1 - Runtime error**: Something went wrong during execution that is not the user's fault. API failures, network errors, missing project config, file system errors, unexpected state. Use `this.output.error(message, {exit: exitCodes.RUNTIME_ERROR})`. | ||
| - **2 - Usage error**: The user provided invalid input to the CLI itself. Bad arguments, unknown flags, invalid flag values, failing input validation. This is oclif's default for `this.output.error()` and all parse errors, so omitting the `exit` option also gives you 2. Use `this.output.error(message, {exit: exitCodes.USAGE_ERROR})` or `this.output.error(message)`. | ||
| - **3 - User abort**: The user declined a confirmation prompt or otherwise chose not to proceed. The command didn't fail, but it also didn't complete its intended action. Use `this.exit(exitCodes.USER_ABORT)`. Import `exitCodes` from `@sanity/cli-core`. | ||
| - **130 - User abort (signal)**: The user cancelled via Ctrl+C or dismissed a prompt without answering. Handled automatically by `SanityCommand.catch()` - commands should not set this manually. | ||
|
|
||
|
|
@@ -197,7 +197,7 @@ Commands use a small set of exit codes aligned with oclif defaults and Unix conv | |
|
|
||
| ### In Practice | ||
|
|
||
| - For `this.output.error()`: pass `{exit: 1}` for runtime errors, `{exit: 2}` (or omit) for usage errors. | ||
| - For `this.output.error()`: pass `{exit: exitCodes.RUNTIME_ERROR}` for runtime errors, `{exit: exitCodes.USAGE_ERROR}` (or omit) for usage errors. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :chefs-kiss: |
||
| - For user-declined prompts: `this.exit(exitCodes.USER_ABORT)` after logging a message like "Deploy cancelled." | ||
| - For custom error classes extending `CLIError`: set `exit` in the constructor options. | ||
| - For `this.exit()`: only use for early termination (exit 0 for success, exit 1 for programmatic failure like `doctor` checks failing). | ||
|
|
@@ -379,7 +379,7 @@ Sanity CLI commands all extend from the `SanityCommand` class, which provides se | |
| ### Basic Command Structure | ||
|
|
||
| ```typescript | ||
| import {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core' | ||
| import {exitCodes, getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core' | ||
| import {Args, Flags, type FlagInput} from '@oclif/core' | ||
|
|
||
| const debug = subdebug('namespace:command') | ||
|
|
@@ -430,7 +430,7 @@ export class MyCommand extends SanityCommand<typeof MyCommand> { | |
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : 'Unknown error' | ||
| debug('Operation failed', error) | ||
| this.output.error(`Failed: ${message}`, {exit: 1}) | ||
| this.output.error(`Failed: ${message}`, {exit: exitCodes.RUNTIME_ERROR}) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -439,7 +439,7 @@ export class MyCommand extends SanityCommand<typeof MyCommand> { | |
| ### Error Handling Pattern | ||
|
|
||
| ```typescript | ||
| import {subdebug} from '@sanity/cli-core' | ||
| import {exitCodes, subdebug} from '@sanity/cli-core' | ||
|
|
||
| const debug = subdebug('feature:action') | ||
|
|
||
|
|
@@ -452,7 +452,7 @@ try { | |
|
|
||
| // Show user-friendly message | ||
| const message = error instanceof Error ? error.message : 'Unknown error' | ||
| this.output.error(`User-facing message: ${message}`, {exit: 1}) | ||
| this.output.error(`User-facing message: ${message}`, {exit: exitCodes.RUNTIME_ERROR}) | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huge win for future agentic work! thanks for including these docs ❤️