Skip to content

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

Open
olabie2 wants to merge 1 commit into
docker:masterfrom
olabie2:6973-print-error-before-plugin-hooks
Open

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

Conversation

@olabie2
Copy link
Copy Markdown

@olabie2 olabie2 commented May 11, 2026

Plugin hook output (such as Gordon's "What's next:" hint) was rendered before the command's own error message because hooks were invoked inside runDocker while the error was only printed in main() after runDocker returned.

Print the error to stderr before invoking the hooks, and replace the error with a status-only cli.StatusError so main() does not print the same message a second time. The original error message is captured up-front and still passed to the plugin hooks.

Closes #6973

- What I did

Fixed the rendering order in cmd/docker/docker.go so that plugin hook output (for example, the ai plugin's "What's next: Debug this container error with Gordon …" hint) is printed after the command's own error output instead of before it.

- How I did it

In runDocker, the flow used to be:

  1. cmd.ExecuteContext(ctx) returned an err.
  2. Plugin hooks were invoked, writing "\nWhat's next:\n …" to dockerCli.Err().
  3. runDocker returned err to main(), which then wrote the error message to os.Stderr.

So the actual stderr order was: hook output → error message.

The fix:

  • Captures the original error message via cmdErrorMessage(err) into a local errMessage before mutating err.
  • Prints err to dockerCli.Err() immediately after cmd.ExecuteContext, gated on the same conditions main() uses (!errdefs.IsCanceled, not errCtxSignalTerminated, non-empty err.Error()), so behaviour for cancellation, signal termination, and exit-code-only errors is unchanged.
  • Replaces err with cli.StatusError{StatusCode: getExitCode(err)} (preserving the exit code) so main() does not print the same message a second time.
  • Passes the captured errMessage to RunCLICommandHooks, so plugin hooks still receive the real error text rather than a synthetic "exited with code N" derived from the replaced error.

- How to verify it

With hooks enabled (features.hooks: "true" in ~/.docker/config.json or DOCKER_CLI_HOOKS=true) and a plugin registering an error-hooks entry for run (Docker Desktop provides this via the ai plugin):

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

Before this change:

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

After this change:

docker: open ./no-such-file: no such file or directory

Run 'docker run --help' for more information

What's next:
    Debug this container error with Gordon → docker ai "help me fix this container error"

Exit code is preserved (125 in this example). All existing tests in ./cmd/docker/... and ./cli-plugins/... still pass.

- Human readable description for the release notes

Print plugin hook output (e.g. the "What's next:" hint) after the command's error message instead of before it

- A picture of a cute animal (not mandatory but encouraged)

🐳

Plugin hook output (such as Gordon's "What's next:" hint) was rendered
before the command's own error message because hooks were invoked inside
runDocker while the error was only printed in main() after runDocker
returned.

Print the error to stderr before invoking the hooks, and replace the
error with a status-only StatusError so main() does not print the same
message a second time. The original error message is captured up-front
and still passed to the plugin hooks.

Closes docker#6973

Signed-off-by: Mohammed Olabie <olabiedev@gmail.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/docker/docker.go 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@thaJeztah thaJeztah added this to the 29.5.0 milestone May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error hooks are printed before command output

3 participants