Skip to content

Commit c373c61

Browse files
committed
cmd/docker: print command error before running plugin hooks
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 #6973 Signed-off-by: Mohammed Olabie <olabiedev@gmail.com>
1 parent ba93f0d commit c373c61

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

cmd/docker/docker.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,24 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
543543
cmd.SetArgs(args)
544544
err = cmd.ExecuteContext(ctx)
545545

546+
// Capture the error message before we may consume the error below;
547+
// plugin hooks still need to see the original message.
548+
errMessage := cmdErrorMessage(err)
549+
550+
// Print the command's error before invoking plugin hooks so that the
551+
// "What's next" hint (and any other hook output) is rendered after
552+
// the command's error output, not before it.
553+
if err != nil && !errdefs.IsCanceled(err) && !errors.As(err, &errCtxSignalTerminated{}) && err.Error() != "" {
554+
_, _ = fmt.Fprintln(dockerCli.Err(), err)
555+
// Replace the error with a status-only one so that main()
556+
// does not print the same message a second time.
557+
err = cli.StatusError{StatusCode: getExitCode(err)}
558+
}
559+
546560
// If the command is being executed in an interactive terminal
547561
// and hook are enabled, run the plugin hooks.
548562
if subCommand != nil && dockerCli.Out().IsTerminal() && dockerCli.HooksEnabled() {
549-
pluginmanager.RunCLICommandHooks(ctx, dockerCli, cmd, subCommand, cmdErrorMessage(err))
563+
pluginmanager.RunCLICommandHooks(ctx, dockerCli, cmd, subCommand, errMessage)
550564
}
551565

552566
return err

0 commit comments

Comments
 (0)