@@ -69,6 +69,10 @@ func (errUnknown) Error() string { return "unknown" }
69
69
70
70
func (errUnknown ) Unknown () {}
71
71
72
+ func (e errUnknown ) WithMessage (msg string ) error {
73
+ return customMessage {e , msg }
74
+ }
75
+
72
76
// unknown maps to Moby's "ErrUnknown"
73
77
type unknown interface {
74
78
Unknown ()
@@ -86,6 +90,10 @@ func (errInvalidArgument) Error() string { return "invalid argument" }
86
90
87
91
func (errInvalidArgument ) InvalidParameter () {}
88
92
93
+ func (e errInvalidArgument ) WithMessage (msg string ) error {
94
+ return customMessage {e , msg }
95
+ }
96
+
89
97
// invalidParameter maps to Moby's "ErrInvalidParameter"
90
98
type invalidParameter interface {
91
99
InvalidParameter ()
@@ -113,6 +121,10 @@ func (errNotFound) Error() string { return "not found" }
113
121
114
122
func (errNotFound ) NotFound () {}
115
123
124
+ func (e errNotFound ) WithMessage (msg string ) error {
125
+ return customMessage {e , msg }
126
+ }
127
+
116
128
// notFound maps to Moby's "ErrNotFound"
117
129
type notFound interface {
118
130
NotFound ()
@@ -127,6 +139,10 @@ type errAlreadyExists struct{}
127
139
128
140
func (errAlreadyExists ) Error () string { return "already exists" }
129
141
142
+ func (e errAlreadyExists ) WithMessage (msg string ) error {
143
+ return customMessage {e , msg }
144
+ }
145
+
130
146
// IsAlreadyExists returns true if the error is due to an already existing
131
147
// metadata item
132
148
func IsAlreadyExists (err error ) bool {
@@ -137,6 +153,10 @@ type errPermissionDenied struct{}
137
153
138
154
func (errPermissionDenied ) Error () string { return "permission denied" }
139
155
156
+ func (e errPermissionDenied ) WithMessage (msg string ) error {
157
+ return customMessage {e , msg }
158
+ }
159
+
140
160
// forbidden maps to Moby's "ErrForbidden"
141
161
type forbidden interface {
142
162
Forbidden ()
@@ -152,6 +172,10 @@ type errResourceExhausted struct{}
152
172
153
173
func (errResourceExhausted ) Error () string { return "resource exhausted" }
154
174
175
+ func (e errResourceExhausted ) WithMessage (msg string ) error {
176
+ return customMessage {e , msg }
177
+ }
178
+
155
179
// IsResourceExhausted returns true if the error is due to
156
180
// a lack of resources or too many attempts.
157
181
func IsResourceExhausted (err error ) bool {
@@ -162,6 +186,10 @@ type errFailedPrecondition struct{}
162
186
163
187
func (e errFailedPrecondition ) Error () string { return "failed precondition" }
164
188
189
+ func (e errFailedPrecondition ) WithMessage (msg string ) error {
190
+ return customMessage {e , msg }
191
+ }
192
+
165
193
// IsFailedPrecondition returns true if an operation could not proceed due to
166
194
// the lack of a particular condition
167
195
func IsFailedPrecondition (err error ) bool {
@@ -174,6 +202,10 @@ func (errConflict) Error() string { return "conflict" }
174
202
175
203
func (errConflict ) Conflict () {}
176
204
205
+ func (e errConflict ) WithMessage (msg string ) error {
206
+ return customMessage {e , msg }
207
+ }
208
+
177
209
// conflict maps to Moby's "ErrConflict"
178
210
type conflict interface {
179
211
Conflict ()
@@ -191,6 +223,10 @@ func (errNotModified) Error() string { return "not modified" }
191
223
192
224
func (errNotModified ) NotModified () {}
193
225
226
+ func (e errNotModified ) WithMessage (msg string ) error {
227
+ return customMessage {e , msg }
228
+ }
229
+
194
230
// notModified maps to Moby's "ErrNotModified"
195
231
type notModified interface {
196
232
NotModified ()
@@ -206,6 +242,10 @@ type errAborted struct{}
206
242
207
243
func (errAborted ) Error () string { return "aborted" }
208
244
245
+ func (e errAborted ) WithMessage (msg string ) error {
246
+ return customMessage {e , msg }
247
+ }
248
+
209
249
// IsAborted returns true if an operation was aborted.
210
250
func IsAborted (err error ) bool {
211
251
return errors .Is (err , errAborted {})
@@ -215,6 +255,10 @@ type errOutOfRange struct{}
215
255
216
256
func (errOutOfRange ) Error () string { return "out of range" }
217
257
258
+ func (e errOutOfRange ) WithMessage (msg string ) error {
259
+ return customMessage {e , msg }
260
+ }
261
+
218
262
// IsOutOfRange returns true if an operation could not proceed due
219
263
// to data being out of the expected range.
220
264
func IsOutOfRange (err error ) bool {
@@ -227,6 +271,10 @@ func (errNotImplemented) Error() string { return "not implemented" }
227
271
228
272
func (errNotImplemented ) NotImplemented () {}
229
273
274
+ func (e errNotImplemented ) WithMessage (msg string ) error {
275
+ return customMessage {e , msg }
276
+ }
277
+
230
278
// notImplemented maps to Moby's "ErrNotImplemented"
231
279
type notImplemented interface {
232
280
NotImplemented ()
@@ -243,6 +291,10 @@ func (errInternal) Error() string { return "internal" }
243
291
244
292
func (errInternal ) System () {}
245
293
294
+ func (e errInternal ) WithMessage (msg string ) error {
295
+ return customMessage {e , msg }
296
+ }
297
+
246
298
// system maps to Moby's "ErrSystem"
247
299
type system interface {
248
300
System ()
@@ -259,6 +311,10 @@ func (errUnavailable) Error() string { return "unavailable" }
259
311
260
312
func (errUnavailable ) Unavailable () {}
261
313
314
+ func (e errUnavailable ) WithMessage (msg string ) error {
315
+ return customMessage {e , msg }
316
+ }
317
+
262
318
// unavailable maps to Moby's "ErrUnavailable"
263
319
type unavailable interface {
264
320
Unavailable ()
@@ -275,6 +331,10 @@ func (errDataLoss) Error() string { return "data loss" }
275
331
276
332
func (errDataLoss ) DataLoss () {}
277
333
334
+ func (e errDataLoss ) WithMessage (msg string ) error {
335
+ return customMessage {e , msg }
336
+ }
337
+
278
338
// dataLoss maps to Moby's "ErrDataLoss"
279
339
type dataLoss interface {
280
340
DataLoss ()
@@ -291,6 +351,10 @@ func (errUnauthorized) Error() string { return "unauthorized" }
291
351
292
352
func (errUnauthorized ) Unauthorized () {}
293
353
354
+ func (e errUnauthorized ) WithMessage (msg string ) error {
355
+ return customMessage {e , msg }
356
+ }
357
+
294
358
// unauthorized maps to Moby's "ErrUnauthorized"
295
359
type unauthorized interface {
296
360
Unauthorized ()
@@ -307,6 +371,8 @@ func isInterface[T any](err error) bool {
307
371
switch x := err .(type ) {
308
372
case T :
309
373
return true
374
+ case customMessage :
375
+ err = x .err
310
376
case interface { Unwrap () error }:
311
377
err = x .Unwrap ()
312
378
if err == nil {
@@ -324,3 +390,22 @@ func isInterface[T any](err error) bool {
324
390
}
325
391
}
326
392
}
393
+
394
+ // customMessage is used to provide a defined error with a custom message.
395
+ // The message is not wrapped but can be compared by the `Is(error) bool` interface.
396
+ type customMessage struct {
397
+ err error
398
+ msg string
399
+ }
400
+
401
+ func (c customMessage ) Is (err error ) bool {
402
+ return c .err == err
403
+ }
404
+
405
+ func (c customMessage ) As (target any ) bool {
406
+ return errors .As (c .err , target )
407
+ }
408
+
409
+ func (c customMessage ) Error () string {
410
+ return c .msg
411
+ }
0 commit comments