|
1 | 1 | import protocol Foundation.LocalizedError
|
2 |
| -import class Foundation.Thread |
3 | 2 |
|
4 |
| -/// Errors thrown while working with Redis. |
5 |
| -public struct RedisError: CustomDebugStringConvertible, CustomStringConvertible, LocalizedError { |
6 |
| - public let description: String |
7 |
| - public let debugDescription: String |
| 3 | +/// When working with NIORedis, several errors are thrown to indicate problems |
| 4 | +/// with state, assertions, or otherwise. |
| 5 | +public enum NIORedisError: LocalizedError { |
| 6 | + case connectionClosed |
| 7 | + case responseConversion(to: Any.Type) |
| 8 | + case unsupportedOperation(method: StaticString, message: String) |
| 9 | + case assertionFailure(message: String) |
8 | 10 |
|
9 |
| - public init( |
10 |
| - identifier: String, |
11 |
| - reason: String, |
12 |
| - file: StaticString = #file, |
13 |
| - function: StaticString = #function, |
14 |
| - line: UInt = #line |
15 |
| - ) { |
16 |
| - let name = String(describing: type(of: self)) |
17 |
| - description = "⚠️ [\(name).\(identifier): \(reason)]" |
18 |
| - debugDescription = "⚠️ Redis Error: \(reason)\n- id: \(name).\(identifier)\n\n\(file): L\(line) - \(function)\n\n\(Thread.callStackSymbols)" |
| 11 | + public var errorDescription: String? { |
| 12 | + let message: String |
| 13 | + switch self { |
| 14 | + case .connectionClosed: message = "Connection was closed while trying to send command." |
| 15 | + case let .responseConversion(type): message = "Failed to convert RESP to \(type)" |
| 16 | + case let .unsupportedOperation(method, helpText): message = "\(method) - \(helpText)" |
| 17 | + case let .assertionFailure(text): message = text |
| 18 | + } |
| 19 | + return "NIORedis: \(message)" |
19 | 20 | }
|
20 | 21 | }
|
21 | 22 |
|
22 |
| -extension RedisError { |
23 |
| - internal static var connectionClosed: RedisError { |
24 |
| - return RedisError(identifier: "connection", reason: "Connection was closed while trying to execute.") |
25 |
| - } |
| 23 | +/// When sending commands to a Redis server, errors caught will be returned as an error message. |
| 24 | +/// These messages are represented by `RedisError` instances. |
| 25 | +public struct RedisError: LocalizedError { |
| 26 | + public let message: String |
| 27 | + |
| 28 | + public var errorDescription: String? { return message } |
26 | 29 |
|
27 |
| - internal static func respConversion<T>(to dest: T.Type) -> RedisError { |
28 |
| - return RedisError(identifier: "respConversion", reason: "Failed to convert RESP to \(String(describing: dest))") |
| 30 | + public init(reason: String) { |
| 31 | + message = "Redis: \(reason)" |
29 | 32 | }
|
30 | 33 | }
|
0 commit comments