Skip to content

Commit 5688097

Browse files
committed
Add RedisError parsing and usage
1 parent 8567231 commit 5688097

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

Sources/NIORedis/Coders/RedisDataDecoder.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ extension RedisDataDecoder {
6161
return try _parseBulkString(at: &position, from: buffer)
6262
case .asterisk:
6363
return try _parseArray(at: &position, from: buffer)
64-
default: return .notYetParsed
64+
case .hyphen:
65+
guard let string = try _parseSimpleString(at: &position, from: buffer) else { return .notYetParsed }
66+
let error = RedisError(identifier: "serverSide", reason: string)
67+
return .parsed
68+
default:
69+
throw RedisError(
70+
identifier: "invalidTokenType",
71+
reason: "Unexpected error while parsing Redis RESP."
72+
)
6573
}
6674
}
6775

@@ -101,8 +109,10 @@ extension RedisDataDecoder {
101109
guard let string = try _parseSimpleString(at: &position, from: buffer) else { return nil }
102110

103111
guard let number = Int(string) else {
104-
#warning("TODO: Throw errors")
105-
return nil
112+
throw RedisError(
113+
identifier: "parseInteger",
114+
reason: "Unexpected error while parsing Redis RESP."
115+
)
106116
}
107117

108118
return number

Sources/NIORedis/RedisError.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
3+
/// Errors thrown while working with Redis.
4+
public struct RedisError: CustomDebugStringConvertible, CustomStringConvertible, LocalizedError {
5+
public private(set) var description: String
6+
public private(set) var debugDescription: String
7+
8+
public init(
9+
identifier: String,
10+
reason: String,
11+
file: String = #file,
12+
function: String = #function,
13+
line: UInt = #line,
14+
column: UInt = #column
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\(Thread.callStackSymbols)"
19+
}
20+
}

Tests/NIORedisTests/RedisDataDecoder+ParsingTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ final class RedisDataDecoderParsingTests: XCTestCase {
5353
try parseTest_recursive(withChunks: ["*-1\r".convertedToData(), "\n".convertedToData()])
5454
}
5555

56+
func testParsing_withInvalidSymbol_throws() {
57+
var buffer = allocator.buffer(capacity: 1)
58+
buffer.write(string: "&3\r\n")
59+
var position = 0
60+
XCTAssertNil(try? RedisDataDecoder()._parse(at: &position, from: buffer))
61+
}
62+
5663
/// See parse_Test_singleValue(input:) String
5764
private func parseTest_singleValue(input: String) throws {
5865
try parseTest_singleValue(input: input.convertedToData())
@@ -298,6 +305,7 @@ extension RedisDataDecoderParsingTests {
298305
("testParsing_with_bulkString_recursively", testParsing_with_bulkString_recursively),
299306
("testParsing_with_arrays", testParsing_with_arrays),
300307
("testParsing_with_arrays_recursively", testParsing_with_arrays_recursively),
308+
("testParsing_withInvalidSymbol_throws", testParsing_withInvalidSymbol_throws),
301309
("testParsing_simpleString_missingEndings_returnsNil", testParsing_simpleString_missingEndings_returnsNil),
302310
("testParsing_simpleString_withNoContent_returnsEmpty", testParsing_simpleString_withNoContent_returnsEmpty),
303311
("testParsing_simpleString_withContent_returnsExpectedContent", testParsing_simpleString_withContent_returnsExpectedContent),

0 commit comments

Comments
 (0)