Browse code

daemon: remove containerNotModifiedError

Removing this type, because:

- containerNotModifiedError is not an actual error, and abstracting it away
was hiding some of these details. It also wasn't used as a sentinel error
anywhere, so doesn't have to be its own type.
- Defining a type just to toggle the error-message between "not running"
and "not stopped" felt a bit over-the-top, as each variant was only used once.
- So "it only had one job", and it didn't even do that right; it produced
capitalized error messages, which makes linters unhappy.

So, let's just inline what it does in the two places it was used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/08/12 04:07:19
Showing 3 changed files
... ...
@@ -59,19 +59,6 @@ func (e nameConflictError) Error() string {
59 59
 
60 60
 func (nameConflictError) Conflict() {}
61 61
 
62
-type containerNotModifiedError struct {
63
-	running bool
64
-}
65
-
66
-func (e containerNotModifiedError) Error() string {
67
-	if e.running {
68
-		return "Container is already started"
69
-	}
70
-	return "Container is already stopped"
71
-}
72
-
73
-func (e containerNotModifiedError) NotModified() {}
74
-
75 62
 type invalidIdentifier string
76 63
 
77 64
 func (e invalidIdentifier) Error() string {
... ...
@@ -30,7 +30,7 @@ func validateState(ctr *container.Container) error {
30 30
 		// already in the desired state. It's implemented as an error
31 31
 		// to make the code calling this function terminate early (as
32 32
 		// no further processing is needed).
33
-		return containerNotModifiedError{running: true}
33
+		return errdefs.NotModified(errors.New("container is already running"))
34 34
 	}
35 35
 	if ctr.RemovalInProgress || ctr.Dead {
36 36
 		return errdefs.Conflict(errors.New("container is marked for removal and cannot be started"))
... ...
@@ -26,7 +26,12 @@ func (daemon *Daemon) ContainerStop(ctx context.Context, name string, options co
26 26
 		return err
27 27
 	}
28 28
 	if !ctr.IsRunning() {
29
-		return containerNotModifiedError{}
29
+		// This is not an actual error, but produces a 304 "not modified"
30
+		// when returned through the API to indicates the container is
31
+		// already in the desired state. It's implemented as an error
32
+		// to make the code calling this function terminate early (as
33
+		// no further processing is needed).
34
+		return errdefs.NotModified(errors.New("container is already stopped"))
30 35
 	}
31 36
 	err = daemon.containerStop(ctx, ctr, options)
32 37
 	if err != nil {