Skip to content

Commit ea8a16a

Browse files
committed
closes #30
1 parent 153b018 commit ea8a16a

File tree

3 files changed

+400
-58
lines changed

3 files changed

+400
-58
lines changed

Sources/SwiftMemcache/MemcachedConnection.swift

Lines changed: 139 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ public actor MemcachedConnection {
5353
case finished
5454
}
5555

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-
6856
private var state: State
6957

7058
/// Initialize a new MemcachedConnection.
@@ -92,7 +80,12 @@ public actor MemcachedConnection {
9280
/// to the server is finished or the task that called this method is cancelled.
9381
public func run() async throws {
9482
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+
)
9689
}
9790

9891
let channel = try await ClientBootstrap(group: eventLoopGroup)
@@ -128,7 +121,12 @@ public actor MemcachedConnection {
128121
case .running:
129122
self.state = .finished
130123
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+
))
132130
case .initial, .finished:
133131
break
134132
}
@@ -151,14 +149,24 @@ public actor MemcachedConnection {
151149
case .enqueued:
152150
break
153151
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+
))
155158
default:
156159
break
157160
}
158161
}
159162

160163
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+
)
162170
}
163171
}
164172

@@ -184,10 +192,20 @@ public actor MemcachedConnection {
184192
if var unwrappedResponse = response {
185193
return Value.readFromBuffer(&unwrappedResponse)
186194
} 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+
)
188201
}
189202
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+
)
191209
}
192210
}
193211

@@ -200,7 +218,7 @@ public actor MemcachedConnection {
200218
/// - Parameters:
201219
/// - key: The key to update the time-to-live for.
202220
/// - 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.
204222
public func touch(_ key: String, newTimeToLive: TimeToLive) async throws {
205223
switch self.state {
206224
case .initial(_, _, _, _),
@@ -215,7 +233,12 @@ public actor MemcachedConnection {
215233
_ = try await self.sendRequest(request)
216234

217235
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+
)
219242
}
220243
}
221244

@@ -229,7 +252,7 @@ public actor MemcachedConnection {
229252
/// - expiration: An optional `TimeToLive` value specifying the TTL (Time-To-Live) for the key-value pair.
230253
/// If provided, the key-value pair will be removed from the cache after the specified TTL duration has passed.
231254
/// 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.
233256
public func set(_ key: String, value: some MemcachedValue, timeToLive: TimeToLive = .indefinitely) async throws {
234257
switch self.state {
235258
case .initial(_, let bufferAllocator, _, _),
@@ -248,7 +271,12 @@ public actor MemcachedConnection {
248271
_ = try await self.sendRequest(request)
249272

250273
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+
)
252280
}
253281
}
254282

@@ -257,8 +285,9 @@ public actor MemcachedConnection {
257285
/// Delete the value for a key from the Memcache server.
258286
///
259287
/// - 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.
262291
public func delete(_ key: String) async throws {
263292
switch self.state {
264293
case .initial(_, _, _, _),
@@ -273,13 +302,28 @@ public actor MemcachedConnection {
273302
case .HD:
274303
return
275304
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+
)
277311
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+
)
279318
}
280319

281320
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+
)
283327
}
284328
}
285329

@@ -290,7 +334,7 @@ public actor MemcachedConnection {
290334
/// - Parameters:
291335
/// - key: The key to prepend the value to.
292336
/// - 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.
294338
public func prepend(_ key: String, value: some MemcachedValue) async throws {
295339
switch self.state {
296340
case .initial(_, let bufferAllocator, _, _),
@@ -309,7 +353,12 @@ public actor MemcachedConnection {
309353
_ = try await self.sendRequest(request)
310354

311355
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+
)
313362
}
314363
}
315364

@@ -320,7 +369,7 @@ public actor MemcachedConnection {
320369
/// - Parameters:
321370
/// - key: The key to append the value to.
322371
/// - 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.
324373
public func append(_ key: String, value: some MemcachedValue) async throws {
325374
switch self.state {
326375
case .initial(_, let bufferAllocator, _, _),
@@ -339,7 +388,12 @@ public actor MemcachedConnection {
339388
_ = try await self.sendRequest(request)
340389

341390
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+
)
343397
}
344398
}
345399

@@ -351,9 +405,9 @@ public actor MemcachedConnection {
351405
/// - Parameters:
352406
/// - key: The key to add the value to.
353407
/// - 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.
357411
public func add(_ key: String, value: some MemcachedValue) async throws {
358412
switch self.state {
359413
case .initial(_, let bufferAllocator, _, _),
@@ -375,13 +429,28 @@ public actor MemcachedConnection {
375429
case .HD:
376430
return
377431
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+
)
379438
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+
)
381445
}
382446

383447
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+
)
385454
}
386455
}
387456

@@ -393,7 +462,7 @@ public actor MemcachedConnection {
393462
/// - Parameters:
394463
/// - key: The key to replace the value for.
395464
/// - 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.
397466
public func replace(_ key: String, value: some MemcachedValue) async throws {
398467
switch self.state {
399468
case .initial(_, let bufferAllocator, _, _),
@@ -415,13 +484,28 @@ public actor MemcachedConnection {
415484
case .HD:
416485
return
417486
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+
)
419493
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+
)
421500
}
422501

423502
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+
)
425509
}
426510
}
427511

@@ -432,7 +516,7 @@ public actor MemcachedConnection {
432516
/// - Parameters:
433517
/// - key: The key for the value to increment.
434518
/// - 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.
436520
public func increment(_ key: String, amount: Int) async throws {
437521
// Ensure the amount is greater than 0
438522
precondition(amount > 0, "Amount to increment should be larger than 0")
@@ -450,7 +534,12 @@ public actor MemcachedConnection {
450534
_ = try await self.sendRequest(request)
451535

452536
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+
)
454543
}
455544
}
456545

@@ -461,7 +550,7 @@ public actor MemcachedConnection {
461550
/// - Parameters:
462551
/// - key: The key for the value to decrement.
463552
/// - 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.
465554
public func decrement(_ key: String, amount: Int) async throws {
466555
// Ensure the amount is greater than 0
467556
precondition(amount > 0, "Amount to decrement should be larger than 0")
@@ -479,7 +568,12 @@ public actor MemcachedConnection {
479568
_ = try await self.sendRequest(request)
480569

481570
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+
)
483577
}
484578
}
485579
}

0 commit comments

Comments
 (0)