DockerCLI/cli
Sebastiaan van Stijn 90058df305
cli/command/container: remove reportError, and put StatusError to use
The `reportError` utility was present because cli.StatusError would print
the error decorated with `Status: <error-message>, Code: <exit-code>`.
That was not desirable in many cases as it would mess-up the output. To
prevent this, the CLI had code to check for an empty `Status` (error message)
in which case the error would be "ignored" (and only used for the exit-status),
and the `reportError` utility would be used to manually print a custom error
message before returning the error.

Now that bca2090061 fixed the output format
of `cli.StatusError`, and 3dd6fc365d and
350a0b68a9 no longer discard these error,
we can get rid of this utility, and just set the error-message for
the status-error.

This patch:

- Introduces a `withHelp` which takes care of decorating errors with
  a "Run --help" hint for the user.
- Introduces a `toStatusError` utility that detects certain errors in
  the container to assign a corresponding exit-code (these error-codes
  can be used to distinguish "client" errors from "container" errors).
- Removes the `reportError` utility, and removes code that manually
  printed errors before returning.

Behavior is mostly unmodified, with the exception of some slight reformatting
of the errors:

- `withHelp` adds a `docker:` prefix to the error, to indicate the error
  is produced by the `docker` command. This prefix was already present
  in most cases.
- The "--help" hint is slightly updated ("Run 'docker run --help' for
  more information" instead of "See 'docker run --help'"), to make it
  more clear that it's a "call to action".
- An empty is added before the "--help" hint to separate it better from
  the error-message.

Before this patch:

    $ docker run --pull=invalid-option alpine
    docker: invalid pull option: 'invalid-option': must be one of "always", "missing" or "never".
    See 'docker run --help'.
    $ echo $?
    125

    $ docker run --rm alpine nosuchcommand
    docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "nosuchcommand": executable file not found in $PATH: unknown.
    $ echo $?
    127

With this patch:

    $ docker run --pull=invalid-option alpine
    docker: invalid pull option: 'invalid-option': must be one of "always", "missing" or "never"

    Run 'docker run --help' for more information
    $ echo $?
    125

    $ docker run --rm alpine nosuchcommand
    docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "nosuchcommand": executable file not found in $PATH: unknown.

    Run 'docker run --help' for more information
    $ echo $?
    127

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-17 15:59:30 +02:00
..
command cli/command/container: remove reportError, and put StatusError to use 2024-07-17 15:59:30 +02:00
compose vendor: github.com/docker/docker 1a1f3cff45ec (master, v27.0-dev) 2024-06-20 13:04:54 +02:00
config test spring-cleaning 2024-07-04 01:35:12 +02:00
connhelper Removed all mentions of "please" from docs and messages 2024-06-11 16:53:40 +02:00
context cli/context/store: Names(): fix panic when called with nil-interface 2024-07-17 01:25:33 +02:00
debug command: include default otel error handler for the cli 2024-04-03 12:01:28 -05:00
flags cli: move "config" flag to cli/flags/ClientOptions.InstallFlags() 2023-06-28 16:04:09 +02:00
hints login: Add message about using PATs 2023-08-03 10:35:04 +02:00
manifest linting: fmt.Sprintf can be replaced with string concatenation (perfsprint) 2024-06-10 21:19:32 +02:00
registry/client use local ConvertToHostname() implementation 2024-06-14 00:25:21 +02:00
streams cli/streams: minor refactoring and docs touch-ups 2023-04-03 11:27:26 +02:00
trust chore: remove deprecated DualStack field 2024-04-15 17:53:35 +08:00
version Move versioning variables to a separate package. 2019-01-29 11:26:40 +00:00
winresources Use goversioninfo to create Windows Version Info 2021-10-11 16:54:22 +02:00
cobra.go cli: FlagErrorFunc: don't print long usage output for invalid flags 2024-07-17 13:22:31 +02:00
cobra_test.go cli: use custom annotation for aliases 2022-06-28 17:32:09 +02:00
error.go cli: make cli.StatusError slightly prettier 2024-07-04 22:08:18 +02:00
required.go cli: improve argument validation output 2024-07-05 03:35:14 +02:00
required_test.go cli: improve argument validation output 2024-07-05 03:35:14 +02:00