Skip to content

cmd/docker: print command error before running plugin hooks - #7228

Open
adisivaprasad wants to merge 1 commit into
docker:masterfrom
adisivaprasad:fix-6973-error-before-hooks
Open

cmd/docker: print command error before running plugin hooks#7228
adisivaprasad wants to merge 1 commit into
docker:masterfrom
adisivaprasad:fix-6973-error-before-hooks

Conversation

@adisivaprasad

Copy link
Copy Markdown

- What I did

Fixes #6973 — when a command fails and a plugin error-hook (or hook) produces a "What's next" hint, the hint was printed before the command's error message:

$ docker run --rm --env-file=./no-such-file alpine

What's next:
    Debug this container error with Gordon → docker ai "help me fix this container error"
docker: open ./no-such-file: no such file or directory

Run 'docker run --help' for more information

- How I did it

The cobra tree runs with SilenceErrors: true, so the error is only printed by main() after runDocker() returns, while plugin hooks run inside runDocker() right after cmd.ExecuteContext() — hence the inverted order.

In runDocker(), before invoking the hooks I now print the pending (non-canceled, non-silent) command error to stderr, and replace the returned error with a bare cli.StatusError carrying the exit code only, so main() doesn't print it a second time. Canceled and signal-terminated errors (which print nothing) pass through untouched, and exit codes are unchanged.

- How to verify it

Repro for that area is in the issue. I verified end-to-end with a stub hook plugin (docker-demo CLI plugin answering docker-cli-plugin-hooks with a fixed next-steps template) configured with "error-hooks": "run", DOCKER_CLI_HOOKS=true, under a pty:

Before (current master build):

$ docker run --rm --env-file=./no-such-file alpine

What's next:
    Try the --debug flag to diagnose (fake hook hint)
docker: --env-file: open ./no-such-file: no such file or directory

Run 'docker run --help' for more information

After (this patch):

$ docker run --rm --env-file=./no-such-file alpine
docker: --env-file: open ./no-such-file: no such file or directory

Run 'docker run --help' for more information

What's next:
    Try the --debug flag to diagnose (fake hook hint)
  • Exit code is 125 both before and after (same as unpatched).
  • Success-case hooks still print after command output (verified with a hooks: context config on docker context ls).
  • go test ./cmd/docker/... ./cli-plugins/... passes.
Fixed plugin hook messages (e.g. "What's next" hints) being printed before the command's error message when a command fails

When a CLI command fails and an error-hook (or hook) produces a
"What's next" hint, the hint was printed before the command's error
message. The command's error is only printed by main() after
runDocker() returns, but plugin hooks run inside runDocker() right
after cmd.ExecuteContext().

Print the pending command error to stderr before invoking the hooks,
and replace the returned error with a silent cli.StatusError that only
carries the exit code, so that main() does not print it a second time.
Canceled and signal-terminated errors (which print nothing) are left
untouched, and exit codes are unchanged.

Signed-off-by: adisivaprasad <adisivaprasad@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error hooks are printed before command output

1 participant