@@ -53,18 +53,6 @@ public actor MemcachedConnection {
53
53
case finished
54
54
}
55
55
56
- /// Enum representing the possible errors that can be encountered in `MemcachedConnection`.
57
- enum MemcachedConnectionError : Error {
58
- /// Indicates that the connection has shut down.
59
- case connectionShutdown
60
- /// Indicates that a nil response was received from the server.
61
- case unexpectedNilResponse
62
- /// Indicates that the key was not found.
63
- case keyNotFound
64
- /// Indicates that the key already exist
65
- case keyExist
66
- }
67
-
68
56
private var state : State
69
57
70
58
/// Initialize a new MemcachedConnection.
@@ -92,7 +80,12 @@ public actor MemcachedConnection {
92
80
/// to the server is finished or the task that called this method is cancelled.
93
81
public func run( ) async throws {
94
82
guard case . initial( let eventLoopGroup, let bufferAllocator, let stream, let continuation) = state else {
95
- throw MemcachedConnectionError . connectionShutdown
83
+ throw MemcachedError (
84
+ code: . connectionShutdown,
85
+ message: " The connection to the Memcached server has shut down. " ,
86
+ cause: nil ,
87
+ location: MemcachedError . SourceLocation. here ( )
88
+ )
96
89
}
97
90
98
91
let channel = try await ClientBootstrap ( group: eventLoopGroup)
@@ -128,7 +121,12 @@ public actor MemcachedConnection {
128
121
case . running:
129
122
self . state = . finished
130
123
requestContinuation. finish ( )
131
- continuation. resume ( throwing: error)
124
+ continuation. resume ( throwing: MemcachedError (
125
+ code: . connectionShutdown,
126
+ message: " The connection to the Memcached server has shut down while processing a request. " ,
127
+ cause: error,
128
+ location: MemcachedError . SourceLocation. here ( )
129
+ ) )
132
130
case . initial, . finished:
133
131
break
134
132
}
@@ -151,14 +149,24 @@ public actor MemcachedConnection {
151
149
case . enqueued:
152
150
break
153
151
case . dropped, . terminated:
154
- continuation. resume ( throwing: MemcachedConnectionError . connectionShutdown)
152
+ continuation. resume ( throwing: MemcachedError (
153
+ code: . connectionShutdown,
154
+ message: " Unable to enqueue request due to the connection being dropped or terminated. " ,
155
+ cause: nil ,
156
+ location: MemcachedError . SourceLocation. here ( )
157
+ ) )
155
158
default :
156
159
break
157
160
}
158
161
}
159
162
160
163
case . finished:
161
- throw MemcachedConnectionError . connectionShutdown
164
+ throw MemcachedError (
165
+ code: . connectionShutdown,
166
+ message: " The connection to the Memcached server has shut down. " ,
167
+ cause: nil ,
168
+ location: MemcachedError . SourceLocation. here ( )
169
+ )
162
170
}
163
171
}
164
172
@@ -184,10 +192,20 @@ public actor MemcachedConnection {
184
192
if var unwrappedResponse = response {
185
193
return Value . readFromBuffer ( & unwrappedResponse)
186
194
} else {
187
- throw MemcachedConnectionError . unexpectedNilResponse
195
+ throw MemcachedError (
196
+ code: . unexpectedNilResponse,
197
+ message: " Received an unexpected nil response from the Memcached server. " ,
198
+ cause: nil ,
199
+ location: MemcachedError . SourceLocation. here ( )
200
+ )
188
201
}
189
202
case . finished:
190
- throw MemcachedConnectionError . connectionShutdown
203
+ throw MemcachedError (
204
+ code: . connectionShutdown,
205
+ message: " The connection to the Memcached server has shut down. " ,
206
+ cause: nil ,
207
+ location: MemcachedError . SourceLocation. here ( )
208
+ )
191
209
}
192
210
}
193
211
@@ -200,7 +218,7 @@ public actor MemcachedConnection {
200
218
/// - Parameters:
201
219
/// - key: The key to update the time-to-live for.
202
220
/// - newTimeToLive: The new time-to-live.
203
- /// - Throws: A `MemcachedConnectionError` if the connection is shutdown or if there's an unexpected nil response .
221
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down .
204
222
public func touch( _ key: String , newTimeToLive: TimeToLive ) async throws {
205
223
switch self . state {
206
224
case . initial( _, _, _, _) ,
@@ -215,7 +233,12 @@ public actor MemcachedConnection {
215
233
_ = try await self . sendRequest ( request)
216
234
217
235
case . finished:
218
- throw MemcachedConnectionError . connectionShutdown
236
+ throw MemcachedError (
237
+ code: . connectionShutdown,
238
+ message: " The connection to the Memcached server has shut down. " ,
239
+ cause: nil ,
240
+ location: MemcachedError . SourceLocation. here ( )
241
+ )
219
242
}
220
243
}
221
244
@@ -229,7 +252,7 @@ public actor MemcachedConnection {
229
252
/// - expiration: An optional `TimeToLive` value specifying the TTL (Time-To-Live) for the key-value pair.
230
253
/// If provided, the key-value pair will be removed from the cache after the specified TTL duration has passed.
231
254
/// If not provided, the key-value pair will persist indefinitely in the cache.
232
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
255
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
233
256
public func set( _ key: String , value: some MemcachedValue , timeToLive: TimeToLive = . indefinitely) async throws {
234
257
switch self . state {
235
258
case . initial( _, let bufferAllocator, _, _) ,
@@ -248,7 +271,12 @@ public actor MemcachedConnection {
248
271
_ = try await self . sendRequest ( request)
249
272
250
273
case . finished:
251
- throw MemcachedConnectionError . connectionShutdown
274
+ throw MemcachedError (
275
+ code: . connectionShutdown,
276
+ message: " The connection to the Memcached server has shut down. " ,
277
+ cause: nil ,
278
+ location: MemcachedError . SourceLocation. here ( )
279
+ )
252
280
}
253
281
}
254
282
@@ -257,8 +285,9 @@ public actor MemcachedConnection {
257
285
/// Delete the value for a key from the Memcache server.
258
286
///
259
287
/// - Parameter key: The key of the item to be deleted.
260
- /// - Throws: A `MemcachedConnectionError.connectionShutdown` error if the connection to the Memcache server is shut down.
261
- /// - Throws: A `MemcachedConnectionError.unexpectedNilResponse` error if the key was not found or if an unexpected response code was returned.
288
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
289
+ /// - Throws: A `MemcachedError` with the code `.keyNotFound` if the key was not found.
290
+ /// - Throws: A `MemcachedError` with the code `.unexpectedNilResponse` if an unexpected response code was returned.
262
291
public func delete( _ key: String ) async throws {
263
292
switch self . state {
264
293
case . initial( _, _, _, _) ,
@@ -273,13 +302,28 @@ public actor MemcachedConnection {
273
302
case . HD:
274
303
return
275
304
case . NF:
276
- throw MemcachedConnectionError . keyNotFound
305
+ throw MemcachedError (
306
+ code: . keyNotFound,
307
+ message: " The specified key was not found. " ,
308
+ cause: nil ,
309
+ location: MemcachedError . SourceLocation. here ( )
310
+ )
277
311
default :
278
- throw MemcachedConnectionError . unexpectedNilResponse
312
+ throw MemcachedError (
313
+ code: . unexpectedNilResponse,
314
+ message: " Received an unexpected nil response from the Memcached server. " ,
315
+ cause: nil ,
316
+ location: MemcachedError . SourceLocation. here ( )
317
+ )
279
318
}
280
319
281
320
case . finished:
282
- throw MemcachedConnectionError . connectionShutdown
321
+ throw MemcachedError (
322
+ code: . connectionShutdown,
323
+ message: " The connection to the Memcached server has shut down. " ,
324
+ cause: nil ,
325
+ location: MemcachedError . SourceLocation. here ( )
326
+ )
283
327
}
284
328
}
285
329
@@ -290,7 +334,7 @@ public actor MemcachedConnection {
290
334
/// - Parameters:
291
335
/// - key: The key to prepend the value to.
292
336
/// - value: The `MemcachedValue` to prepend.
293
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
337
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
294
338
public func prepend( _ key: String , value: some MemcachedValue ) async throws {
295
339
switch self . state {
296
340
case . initial( _, let bufferAllocator, _, _) ,
@@ -309,7 +353,12 @@ public actor MemcachedConnection {
309
353
_ = try await self . sendRequest ( request)
310
354
311
355
case . finished:
312
- throw MemcachedConnectionError . connectionShutdown
356
+ throw MemcachedError (
357
+ code: . connectionShutdown,
358
+ message: " The connection to the Memcached server has shut down. " ,
359
+ cause: nil ,
360
+ location: MemcachedError . SourceLocation. here ( )
361
+ )
313
362
}
314
363
}
315
364
@@ -320,7 +369,7 @@ public actor MemcachedConnection {
320
369
/// - Parameters:
321
370
/// - key: The key to append the value to.
322
371
/// - value: The `MemcachedValue` to append.
323
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
372
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
324
373
public func append( _ key: String , value: some MemcachedValue ) async throws {
325
374
switch self . state {
326
375
case . initial( _, let bufferAllocator, _, _) ,
@@ -339,7 +388,12 @@ public actor MemcachedConnection {
339
388
_ = try await self . sendRequest ( request)
340
389
341
390
case . finished:
342
- throw MemcachedConnectionError . connectionShutdown
391
+ throw MemcachedError (
392
+ code: . connectionShutdown,
393
+ message: " The connection to the Memcached server has shut down. " ,
394
+ cause: nil ,
395
+ location: MemcachedError . SourceLocation. here ( )
396
+ )
343
397
}
344
398
}
345
399
@@ -351,9 +405,9 @@ public actor MemcachedConnection {
351
405
/// - Parameters:
352
406
/// - key: The key to add the value to.
353
407
/// - value: The `MemcachedValue` to add.
354
- /// - Throws: A `MemcachedConnectionError .connectionShutdown` if the connection to the Memcached server is shut down.
355
- /// - Throws: A `MemcachedConnectionError .keyExist` if the key already exists in the Memcached server .
356
- /// - Throws: A `MemcachedConnectionError .unexpectedNilResponse` if an unexpected response code is returned.
408
+ /// - Throws: A `MemcachedError` with the code ` .connectionShutdown` if the connection to the Memcache server is shut down.
409
+ /// - Throws: A `MemcachedError` with the code ` .keyExist` if the key already exist .
410
+ /// - Throws: A `MemcachedError` with the code ` .unexpectedNilResponse` if an unexpected response code was returned.
357
411
public func add( _ key: String , value: some MemcachedValue ) async throws {
358
412
switch self . state {
359
413
case . initial( _, let bufferAllocator, _, _) ,
@@ -375,13 +429,28 @@ public actor MemcachedConnection {
375
429
case . HD:
376
430
return
377
431
case . NS:
378
- throw MemcachedConnectionError . keyExist
432
+ throw MemcachedError (
433
+ code: . keyExist,
434
+ message: " The specified key already exist. " ,
435
+ cause: nil ,
436
+ location: MemcachedError . SourceLocation. here ( )
437
+ )
379
438
default :
380
- throw MemcachedConnectionError . unexpectedNilResponse
439
+ throw MemcachedError (
440
+ code: . unexpectedNilResponse,
441
+ message: " Received an unexpected nil response from the Memcached server. " ,
442
+ cause: nil ,
443
+ location: MemcachedError . SourceLocation. here ( )
444
+ )
381
445
}
382
446
383
447
case . finished:
384
- throw MemcachedConnectionError . connectionShutdown
448
+ throw MemcachedError (
449
+ code: . connectionShutdown,
450
+ message: " The connection to the Memcached server has shut down. " ,
451
+ cause: nil ,
452
+ location: MemcachedError . SourceLocation. here ( )
453
+ )
385
454
}
386
455
}
387
456
@@ -393,7 +462,7 @@ public actor MemcachedConnection {
393
462
/// - Parameters:
394
463
/// - key: The key to replace the value for.
395
464
/// - value: The `MemcachedValue` to replace.
396
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
465
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
397
466
public func replace( _ key: String , value: some MemcachedValue ) async throws {
398
467
switch self . state {
399
468
case . initial( _, let bufferAllocator, _, _) ,
@@ -415,13 +484,28 @@ public actor MemcachedConnection {
415
484
case . HD:
416
485
return
417
486
case . NS:
418
- throw MemcachedConnectionError . keyNotFound
487
+ throw MemcachedError (
488
+ code: . keyNotFound,
489
+ message: " The specified key was not found. " ,
490
+ cause: nil ,
491
+ location: MemcachedError . SourceLocation. here ( )
492
+ )
419
493
default :
420
- throw MemcachedConnectionError . unexpectedNilResponse
494
+ throw MemcachedError (
495
+ code: . unexpectedNilResponse,
496
+ message: " Received an unexpected nil response from the Memcached server. " ,
497
+ cause: nil ,
498
+ location: MemcachedError . SourceLocation. here ( )
499
+ )
421
500
}
422
501
423
502
case . finished:
424
- throw MemcachedConnectionError . connectionShutdown
503
+ throw MemcachedError (
504
+ code: . connectionShutdown,
505
+ message: " The connection to the Memcached server has shut down. " ,
506
+ cause: nil ,
507
+ location: MemcachedError . SourceLocation. here ( )
508
+ )
425
509
}
426
510
}
427
511
@@ -432,7 +516,7 @@ public actor MemcachedConnection {
432
516
/// - Parameters:
433
517
/// - key: The key for the value to increment.
434
518
/// - amount: The `Int` amount to increment the value by. Must be larger than 0.
435
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
519
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
436
520
public func increment( _ key: String , amount: Int ) async throws {
437
521
// Ensure the amount is greater than 0
438
522
precondition ( amount > 0 , " Amount to increment should be larger than 0 " )
@@ -450,7 +534,12 @@ public actor MemcachedConnection {
450
534
_ = try await self . sendRequest ( request)
451
535
452
536
case . finished:
453
- throw MemcachedConnectionError . connectionShutdown
537
+ throw MemcachedError (
538
+ code: . connectionShutdown,
539
+ message: " The connection to the Memcached server has shut down. " ,
540
+ cause: nil ,
541
+ location: MemcachedError . SourceLocation. here ( )
542
+ )
454
543
}
455
544
}
456
545
@@ -461,7 +550,7 @@ public actor MemcachedConnection {
461
550
/// - Parameters:
462
551
/// - key: The key for the value to decrement.
463
552
/// - amount: The `Int` amount to decrement the value by. Must be larger than 0.
464
- /// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down.
553
+ /// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
465
554
public func decrement( _ key: String , amount: Int ) async throws {
466
555
// Ensure the amount is greater than 0
467
556
precondition ( amount > 0 , " Amount to decrement should be larger than 0 " )
@@ -479,7 +568,12 @@ public actor MemcachedConnection {
479
568
_ = try await self . sendRequest ( request)
480
569
481
570
case . finished:
482
- throw MemcachedConnectionError . connectionShutdown
571
+ throw MemcachedError (
572
+ code: . connectionShutdown,
573
+ message: " The connection to the Memcached server has shut down. " ,
574
+ cause: nil ,
575
+ location: MemcachedError . SourceLocation. here ( )
576
+ )
483
577
}
484
578
}
485
579
}
0 commit comments