Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: ValidateRestartPolicy: improve errors for invalid policies #46352

Merged
merged 1 commit into from Aug 28, 2023

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Aug 28, 2023

Make the error message slightly clearer on "what" part is not valid, and provide suggestions on what are acceptable values.

Before this change:

docker create --restart=always:3 busybox
Error response from daemon: invalid restart policy: maximum retry count cannot be used with restart policy 'always'

docker create --restart=always:-1 busybox
Error response from daemon: invalid restart policy: maximum retry count cannot be used with restart policy 'always'

docker create --restart=unknown busybox
Error response from daemon: invalid restart policy 'unknown'

After this change:

docker create --restart=always:3 busybox
Error response from daemon: invalid restart policy: maximum retry count can only be used with 'on-failure'

docker create --restart=always:-1 busybox
Error response from daemon: invalid restart policy: maximum retry count can only be used with 'on-failure' and cannot be negative

docker create --restart=unknown busybox
Error response from daemon: invalid restart policy: unknown policy 'unknown'; use one of 'no', 'always', 'on-failure', or 'unless-stopped'

- What I did

- How I did it

- How to verify it

- Description for the changelog

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

@thaJeztah thaJeztah added area/api status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. impact/api impact/changelog area/ux labels Aug 28, 2023
@thaJeztah thaJeztah added this to the 25.0.0 milestone Aug 28, 2023
@thaJeztah
Copy link
Member Author

thaJeztah commented Aug 28, 2023

Actually, I think it can be slightly more terse; reading it now, I see "restart-policy" is repeated in the error.

Let me change "can only be used with the 'on-failure' restart policy" to "can only be used with 'on-failure'"

@thaJeztah
Copy link
Member Author

Updated 👍

@thaJeztah
Copy link
Member Author

Ah, dang. Missed a test somewhere;

=== FAIL: github.com/docker/docker/integration-cli TestDockerAPISuite/TestContainerAPIRestartPolicyRetryMismatch (0.04s)
    docker_api_containers_test.go:761: assertion failed: expression is false: strings.Contains(string(b[:]), "maximum retry count cannot be used with restart policy")

Make the error message slightly clearer on "what" part is not valid,
and provide suggestions on what are acceptable values.

Before this change:

    docker create --restart=always:3 busybox
    Error response from daemon: invalid restart policy: maximum retry count cannot be used with restart policy 'always'

    docker create --restart=always:-1 busybox
    Error response from daemon: invalid restart policy: maximum retry count cannot be used with restart policy 'always'

    docker create --restart=unknown busybox
    Error response from daemon: invalid restart policy 'unknown'

After this change:

    docker create --restart=always:3 busybox
    Error response from daemon: invalid restart policy: maximum retry count can only be used with 'on-failure'

    docker create --restart=always:-1 busybox
    Error response from daemon: invalid restart policy: maximum retry count can only be used with 'on-failure' and cannot be negative

    docker create --restart=unknown busybox
    Error response from daemon: invalid restart policy: unknown policy 'unknown'; use one of 'no', 'always', 'on-failure', or 'unless-stopped'

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
if policy.MaximumRetryCount < 0 {
msg += " and cannot be negative"
}
return &errInvalidParameter{fmt.Errorf(msg)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return &errInvalidParameter{fmt.Errorf(msg)}
return &errInvalidParameter{errors.New(msg)}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes; was considering that, but we didn't import "errors" yet, so thought I'd keep it at fmt.Errorf() 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me :)

@thaJeztah thaJeztah merged commit 7f6dda3 into moby:master Aug 28, 2023
103 checks passed
@thaJeztah thaJeztah deleted the restart_policy_improve_error branch August 28, 2023 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api area/ux impact/api impact/changelog kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants