Commit 13432f0c authored by Nathan Harris's avatar Nathan Harris

Rename `RedisNIOError` to `RedisClientError`

Motivation:

To make it a little more generic, and to avoid turnover during renames (such as the planned rebranding in issue #61), `RedisClientError` more accurately reflects the source of the errors, as well as the responsibility of causing the bug.

Modifications:

- Rename `RedisNIOError` to `RedisClientError`
- Rename `RedisError` file to `RedisErrors`
- Add documentation of `RedisClientError`
- Remove no longer used `.unsupportedOperation(method:message:)` value
- Rename `.responseConversion(to:)` to `.failedRESPConversion(to:)`

Result:

Names of `RedisClientError` should be more descriptive, less prone to turnover, and more documented for users to understand the issues related to these thrown errors.
parent 6c39eb39
Pipeline #70000464 passed with stages
in 4 minutes and 37 seconds
......@@ -160,7 +160,7 @@ extension RedisClient {
let value = result[0].string,
let position = Int(value)
else {
throw RedisNIOError.assertionFailure(message: "Unexpected value in response: \(result[0])")
throw RedisClientError.assertionFailure(message: "Unexpected value in response: \(result[0])")
}
return position
}
......
......@@ -442,11 +442,11 @@ extension RedisClient {
.flatMapThrowing {
guard !$0.isNull else { return nil }
guard let response = [RESPValue](fromRESP: $0) else {
throw RedisNIOError.responseConversion(to: [RESPValue].self)
throw RedisClientError.failedRESPConversion(to: [RESPValue].self)
}
assert(response.count == 2, "Unexpected response size returned!")
guard let key = response[0].string else {
throw RedisNIOError.assertionFailure(message: "Unexpected structure in response: \(response)")
throw RedisClientError.assertionFailure(message: "Unexpected structure in response: \(response)")
}
return (key, response[1])
}
......
......@@ -31,7 +31,7 @@ extension RedisClient {
let scoreItem = response[scoreIsFirst ? index : index + 1]
guard let score = Double(fromRESP: scoreItem) else {
throw RedisNIOError.assertionFailure(message: "Unexpected response: '\(scoreItem)'")
throw RedisClientError.assertionFailure(message: "Unexpected response: '\(scoreItem)'")
}
let elementIndex = scoreIsFirst ? index + 1 : index
......@@ -456,14 +456,14 @@ extension RedisClient {
.flatMapThrowing {
guard !$0.isNull else { return nil }
guard let response = [RESPValue](fromRESP: $0) else {
throw RedisNIOError.responseConversion(to: [RESPValue].self)
throw RedisClientError.failedRESPConversion(to: [RESPValue].self)
}
assert(response.count == 3, "Unexpected response size returned!")
guard
let key = response[0].string,
let score = Double(fromRESP: response[1])
else {
throw RedisNIOError.assertionFailure(message: "Unexpected structure in response: \(response)")
throw RedisClientError.assertionFailure(message: "Unexpected structure in response: \(response)")
}
return (key, score, response[2])
}
......
......@@ -30,7 +30,7 @@ extension EventLoopFuture where Value == RESPValue {
{
return self.flatMapThrowing {
guard let value = T(fromRESP: $0) else {
throw RedisNIOError.responseConversion(to: type)
throw RedisClientError.failedRESPConversion(to: type)
}
return value
}
......
......@@ -183,7 +183,7 @@ extension RedisConnection {
/// If a `RedisError` is returned, the future will be failed instead.
public func send(command: String, with arguments: [RESPValue]) -> EventLoopFuture<RESPValue> {
guard self.isConnected else {
let error = RedisNIOError.connectionClosed
let error = RedisClientError.connectionClosed
logger.warning("\(error.localizedDescription)")
return self.channel.eventLoop.makeFailedFuture(error)
}
......
......@@ -14,28 +14,29 @@
import protocol Foundation.LocalizedError
/// When working with RedisNIO, several errors are thrown to indicate problems
/// with state, assertions, or otherwise.
public enum RedisNIOError: LocalizedError {
/// When working with `RedisClient`, runtime errors can be thrown to indicate problems with connection state, decoding assertions, or otherwise.
public enum RedisClientError: LocalizedError {
/// The connection is closed, but was used to try and send a command to Redis.
case connectionClosed
case responseConversion(to: Any.Type)
case unsupportedOperation(method: StaticString, message: String)
/// Conversion from `RESPValue` to the specified type failed.
/// If this is ever triggered, please capture the original `RESPValue` value.
case failedRESPConversion(to: Any.Type)
/// Expectations of message structures were not met.
/// If this is ever triggered, please capture the original byte message from Redis along with the command and arguments to Redis.
case assertionFailure(message: String)
public var errorDescription: String? {
let message: String
switch self {
case .connectionClosed: message = "Connection was closed while trying to send command."
case let .responseConversion(type): message = "Failed to convert RESP to \(type)"
case let .unsupportedOperation(method, helpText): message = "\(method) - \(helpText)"
case let .failedRESPConversion(type): message = "Failed to convert RESP to \(type)"
case let .assertionFailure(text): message = text
}
return "RedisNIO: \(message)"
}
}
/// When sending commands to a Redis server, errors caught will be returned as an error message.
/// These messages are represented by `RedisError` instances.
/// If something goes wrong with a command within Redis, it will respond with an error that is captured and represented by instances of this type.
public struct RedisError: LocalizedError {
public let message: String
......
......@@ -42,7 +42,7 @@ final class RedisConnectionTests: RedisIntegrationTestCase {
_ = try self.connection.ping().wait()
XCTFail("ping() should throw when connection is closed.")
} catch {
XCTAssertTrue(error is RedisNIOError)
XCTAssertTrue(error is RedisClientError)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment