Merge pull request #5231 from thaJeztah/prettier_exit_status

cli: make cli.StatusError slightly prettier
This commit is contained in:
Sebastiaan van Stijn 2024-07-05 10:50:46 +02:00 committed by GitHub
commit cad08ff3b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 4 deletions

View File

@ -90,7 +90,7 @@ func TestNetworkRemoveForce(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
} else { } else {
assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr)) assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr))
assert.ErrorContains(t, err, "Code: 1") assert.ErrorContains(t, err, "exit status 1")
} }
}) })
} }

View File

@ -444,7 +444,7 @@ func TestFormatInfo(t *testing.T) {
{ {
doc: "syntax", doc: "syntax",
template: "{{}", template: "{{}",
expectedError: `Status: template parsing error: template: :1: unexpected "}" in command, Code: 64`, expectedError: `template parsing error: template: :1: unexpected "}" in command`,
}, },
{ {
doc: "syntax", doc: "syntax",

View File

@ -1,7 +1,7 @@
package cli package cli
import ( import (
"fmt" "strconv"
"strings" "strings"
) )
@ -28,6 +28,12 @@ type StatusError struct {
StatusCode int StatusCode int
} }
// Error formats the error for printing. If a custom Status is provided,
// it is returned as-is, otherwise it generates a generic error-message
// based on the StatusCode.
func (e StatusError) Error() string { func (e StatusError) Error() string {
return fmt.Sprintf("Status: %s, Code: %d", e.Status, e.StatusCode) if e.Status == "" {
return "exit status " + strconv.Itoa(e.StatusCode)
}
return e.Status
} }