Skip to content

Commit bca2090

Browse files
committed
cli: make cli.StatusError slightly prettier
This error didn't do a great job at formatting. If a StatusError was produced without a Status message, it would print a very non-informative error, with information missing. Let's update the output: - If a status-message is provided; print just that (after all the status code is something that can be found from the shell, e.g. through `echo $?` in Bash). - If no status-message is provided: print a message more similar to Go's `exec.ExecError`, which uses `os.rocessState.String()` (see [1]). Before this patch, an error without custom status would print: Status: , Code: 2 After this patch: exit status 2 In situations where a custom error-message is provided, the error-message is print as-is, whereas before this patch, the message got combined with the `Status:` and `Code:`, which resulted in some odd output. Before this patch: docker volume --no-such-flag Status: unknown flag: --no-such-flag See 'docker volume --help'. Usage: docker volume COMMAND Manage volumes Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove unused local volumes rm Remove one or more volumes update Update a volume (cluster volumes only) Run 'docker volume COMMAND --help' for more information on a command. , Code: 125 With this patch, the error is shown as-is; docker volume --no-such-flag unknown flag: --no-such-flag See 'docker volume --help'. Usage: docker volume COMMAND Manage volumes Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove unused local volumes rm Remove one or more volumes update Update a volume (cluster volumes only) Run 'docker volume COMMAND --help' for more information on a command. While the exit-code is no longer printed, it's still properly handled; echo $? 125 [1]: https://github.com/golang/go/blob/82c14346d89ec0eeca114f9ca0e88516b2cda454/src/os/exec_posix.go#L107-L135 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 5aae44b commit bca2090

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

cli/command/network/remove_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestNetworkRemoveForce(t *testing.T) {
9090
assert.NilError(t, err)
9191
} else {
9292
assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr))
93-
assert.ErrorContains(t, err, "Code: 1")
93+
assert.ErrorContains(t, err, "exit status 1")
9494
}
9595
})
9696
}

cli/command/system/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func TestFormatInfo(t *testing.T) {
444444
{
445445
doc: "syntax",
446446
template: "{{}",
447-
expectedError: `Status: template parsing error: template: :1: unexpected "}" in command, Code: 64`,
447+
expectedError: `template parsing error: template: :1: unexpected "}" in command`,
448448
},
449449
{
450450
doc: "syntax",

cli/error.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cli
22

33
import (
4-
"fmt"
4+
"strconv"
55
"strings"
66
)
77

@@ -28,6 +28,12 @@ type StatusError struct {
2828
StatusCode int
2929
}
3030

31+
// Error formats the error for printing. If a custom Status is provided,
32+
// it is returned as-is, otherwise it generates a generic error-message
33+
// based on the StatusCode.
3134
func (e StatusError) Error() string {
32-
return fmt.Sprintf("Status: %s, Code: %d", e.Status, e.StatusCode)
35+
if e.Status == "" {
36+
return "exit status " + strconv.Itoa(e.StatusCode)
37+
}
38+
return e.Status
3339
}

0 commit comments

Comments
 (0)