-
Notifications
You must be signed in to change notification settings - Fork 62
Add a global JSON error envelope with stable codes and next_steps #1282
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
+503
−51
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0300b6e
Add structured JSON next_steps to workflow commands and a global JSON…
no-itsbackpack 86989ca
Defer non-workflow errors to the global JSON classifier in workflow c…
no-itsbackpack 2cac4d5
Only classify transport-level failures as NETWORK_ERROR
no-itsbackpack ab8a035
Restore non-human format rejection for confirmation-gated workflow co…
no-itsbackpack 4b14f75
Merge branch 'main' into cli/workflow-json-next-steps
no-itsbackpack 0651e90
Revert workflow-specific JSON error handling to focus on the global e…
no-itsbackpack b7ce77a
Unify JSON error envelope on the issues schema, strip ANSI codes, doc…
no-itsbackpack 008f9fe
Match both interactive-shell and interactive-terminal messages as TTY…
no-itsbackpack 8220ab3
Error on unknown root subcommands so JSON mode reports UNKNOWN_COMMAND.
no-itsbackpack 838f2a7
Revert "Error on unknown root subcommands so JSON mode reports UNKNOW…
no-itsbackpack 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package workflow | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| ) | ||
|
|
||
| // errorContext carries workflow command context so JSON error responses can | ||
| // include remediation commands with real values instead of placeholders. | ||
| type errorContext struct { | ||
| Org string | ||
| Database string | ||
| Branch string | ||
| Number string | ||
| } | ||
|
|
||
| // handle converts err into a structured JSON error when the printer is in | ||
| // JSON mode; in human mode it returns err unchanged. | ||
| func (c errorContext) handle(ch *cmdutil.Helper, err error) error { | ||
| if err == nil || ch.Printer.Format() != printer.JSON { | ||
| return err | ||
| } | ||
| if cmdutil.ErrCode(err) == ps.ErrNotFound || strings.Contains(err.Error(), "does not exist") { | ||
| return c.report(ch, "error", "NOT_FOUND", err.Error(), c.notFoundNextSteps()) | ||
| } | ||
| return c.report(ch, "error", "WORKFLOW_ERROR", err.Error(), c.defaultNextSteps()) | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // reportConfirmationRequired reports a confirmation-gated action in JSON mode. | ||
| // Callers gate on JSON format before calling. | ||
| func (c errorContext) reportConfirmationRequired(ch *cmdutil.Helper, code, message, retryCmd string) error { | ||
| return c.report(ch, "action_required", code, message, []string{ | ||
| "Ask the user to approve this workflow action", | ||
| retryCmd, | ||
| }) | ||
| } | ||
|
|
||
| func reportMissingCreateFlags(ch *cmdutil.Helper, org, database, branch string, err error) error { | ||
| if ch.Printer.Format() != printer.JSON { | ||
| return err | ||
| } | ||
| c := errorContext{Org: org, Database: database, Branch: branch} | ||
| return c.report(ch, "action_required", "MISSING_FLAGS", err.Error(), []string{ | ||
| cmdutil.AgentKeyspaceListCmd(org, database, branch), | ||
| cmdutil.AgentWorkflowCreateCmd(org, database, branch), | ||
| }) | ||
| } | ||
|
|
||
| func (c errorContext) report(ch *cmdutil.Helper, status, code, message string, nextSteps []string) error { | ||
| payload := map[string]any{ | ||
| "status": status, | ||
| "error": message, | ||
| "issues": []map[string]string{ | ||
| { | ||
| "code": code, | ||
| "message": message, | ||
| }, | ||
| }, | ||
| "next_steps": nextSteps, | ||
| } | ||
| if c.Database != "" { | ||
| payload["database"] = c.Database | ||
| } | ||
| if c.Branch != "" { | ||
| payload["branch"] = c.Branch | ||
| } | ||
| if c.Number != "" { | ||
| payload["workflow_number"] = c.Number | ||
| } | ||
|
|
||
| if err := ch.Printer.PrintJSON(payload); err != nil { | ||
| return err | ||
| } | ||
| exitCode := cmdutil.FatalErrExitCode | ||
| if status == "action_required" { | ||
| exitCode = cmdutil.ActionRequestedExitCode | ||
| } | ||
| return cmdutil.JSONReportedError(exitCode) | ||
| } | ||
|
|
||
| func (c errorContext) notFoundNextSteps() []string { | ||
| steps := []string{ | ||
| cmdutil.AgentDatabaseListCmd(c.Org), | ||
| cmdutil.AgentWorkflowListCmd(c.Org, c.Database), | ||
| } | ||
| if c.Branch != "" { | ||
| steps = append(steps, cmdutil.AgentKeyspaceListCmd(c.Org, c.Database, c.Branch)) | ||
| } | ||
| if c.Number != "" { | ||
| steps = append(steps, cmdutil.AgentWorkflowShowCmd(c.Org, c.Database, c.Number)) | ||
| } | ||
| return steps | ||
| } | ||
|
|
||
| func (c errorContext) defaultNextSteps() []string { | ||
| steps := []string{cmdutil.AgentAuthCheckCmd()} | ||
| if c.Database != "" { | ||
| steps = append(steps, cmdutil.AgentWorkflowListCmd(c.Org, c.Database)) | ||
| if c.Number != "" { | ||
| steps = append(steps, cmdutil.AgentWorkflowShowCmd(c.Org, c.Database, c.Number)) | ||
| } | ||
| } | ||
| return steps | ||
| } | ||
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.