forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
245 lines (186 loc) · 5.57 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package daemon
import (
"fmt"
"strings"
"syscall"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
func errNotRunning(id string) error {
return stateConflictError{errors.Errorf("Container %s is not running", id)}
}
func containerNotFound(id string) error {
return objNotFoundError{"container", id}
}
func volumeNotFound(id string) error {
return objNotFoundError{"volume", id}
}
type objNotFoundError struct {
object string
id string
}
func (e objNotFoundError) Error() string {
return "No such " + e.object + ": " + e.id
}
func (e objNotFoundError) NotFound() {}
type stateConflictError struct {
cause error
}
func (e stateConflictError) Error() string {
return e.cause.Error()
}
func (e stateConflictError) Cause() error {
return e.cause
}
func (e stateConflictError) Conflict() {}
func errContainerIsRestarting(containerID string) error {
cause := errors.Errorf("Container %s is restarting, wait until the container is running", containerID)
return stateConflictError{cause}
}
func errExecNotFound(id string) error {
return objNotFoundError{"exec instance", id}
}
func errExecPaused(id string) error {
cause := errors.Errorf("Container %s is paused, unpause the container before exec", id)
return stateConflictError{cause}
}
func errNotPaused(id string) error {
cause := errors.Errorf("Container %s is already paused", id)
return stateConflictError{cause}
}
type nameConflictError struct {
id string
name string
}
func (e nameConflictError) Error() string {
return fmt.Sprintf("Conflict. The container name %q is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name.", e.name, e.id)
}
func (nameConflictError) Conflict() {}
type validationError struct {
cause error
}
func (e validationError) Error() string {
return e.cause.Error()
}
func (e validationError) InvalidParameter() {}
func (e validationError) Cause() error {
return e.cause
}
type notAllowedError struct {
cause error
}
func (e notAllowedError) Error() string {
return e.cause.Error()
}
func (e notAllowedError) Forbidden() {}
func (e notAllowedError) Cause() error {
return e.cause
}
type containerNotModifiedError struct {
running bool
}
func (e containerNotModifiedError) Error() string {
if e.running {
return "Container is already started"
}
return "Container is already stopped"
}
func (e containerNotModifiedError) NotModified() {}
type systemError struct {
cause error
}
func (e systemError) Error() string {
return e.cause.Error()
}
func (e systemError) SystemError() {}
func (e systemError) Cause() error {
return e.cause
}
type invalidIdentifier string
func (e invalidIdentifier) Error() string {
return fmt.Sprintf("invalid name or ID supplied: %q", string(e))
}
func (invalidIdentifier) InvalidParameter() {}
type duplicateMountPointError string
func (e duplicateMountPointError) Error() string {
return "Duplicate mount point: " + string(e)
}
func (duplicateMountPointError) InvalidParameter() {}
type containerFileNotFound struct {
file string
container string
}
func (e containerFileNotFound) Error() string {
return "Could not find the file " + e.file + " in container " + e.container
}
func (containerFileNotFound) NotFound() {}
type invalidFilter struct {
filter string
value interface{}
}
func (e invalidFilter) Error() string {
msg := "Invalid filter '" + e.filter
if e.value != nil {
msg += fmt.Sprintf("=%s", e.value)
}
return msg + "'"
}
func (e invalidFilter) InvalidParameter() {}
type unknownError struct {
cause error
}
func (e unknownError) Error() string {
return e.cause.Error()
}
func (unknownError) Unknown() {}
func (e unknownError) Cause() error {
return e.cause
}
type startInvalidConfigError string
func (e startInvalidConfigError) Error() string {
return string(e)
}
func (e startInvalidConfigError) InvalidParameter() {} // Is this right???
func translateContainerdStartErr(cmd string, setExitCode func(int), err error) error {
errDesc := grpc.ErrorDesc(err)
contains := func(s1, s2 string) bool {
return strings.Contains(strings.ToLower(s1), s2)
}
var retErr error = unknownError{errors.New(errDesc)}
// if we receive an internal error from the initial start of a container then lets
// return it instead of entering the restart loop
// set to 127 for container cmd not found/does not exist)
if contains(errDesc, cmd) &&
(contains(errDesc, "executable file not found") ||
contains(errDesc, "no such file or directory") ||
contains(errDesc, "system cannot find the file specified")) {
setExitCode(127)
retErr = startInvalidConfigError(errDesc)
}
// set to 126 for container cmd can't be invoked errors
if contains(errDesc, syscall.EACCES.Error()) {
setExitCode(126)
retErr = startInvalidConfigError(errDesc)
}
// attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
if contains(errDesc, syscall.ENOTDIR.Error()) {
errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
setExitCode(127)
retErr = startInvalidConfigError(errDesc)
}
// TODO: it would be nice to get some better errors from containerd so we can return better errors here
return retErr
}
// TODO: cpuguy83 take care of it once the new library is ready
type errNotFound struct{ error }
func (errNotFound) NotFound() {}
func (e errNotFound) Cause() error {
return e.error
}
// notFound is a helper to create an error of the class with the same name from any error type
func notFound(err error) error {
if err == nil {
return nil
}
return errNotFound{err}
}