Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist invalid conversion #34

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "HummingbirdRedis", targets: ["HummingbirdRedis"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"),
.package(url: "https://github.com/swift-server/RediStack.git", from: "1.4.0"),
],
targets: [
Expand Down
6 changes: 5 additions & 1 deletion Sources/HummingbirdRedis/Persist+Redis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public struct RedisPersistDriver: PersistDriver {

/// get value for key
public func get<Object: Codable>(key: String, as object: Object.Type) async throws -> Object? {
try await self.redisConnectionPool.get(.init(key), asJSON: object)
do {
return try await self.redisConnectionPool.get(.init(key), asJSON: object)
} catch is DecodingError {
throw PersistError.invalidConversion
}
}

/// remove key
Expand Down
25 changes: 25 additions & 0 deletions Tests/HummingbirdRedisTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ final class PersistTests: XCTestCase {
}
}

func testInvalidGetAs() async throws {
struct TestCodable: Codable {
let buffer: String
}
let app = try self.createApplication { router, persist in
router.put("/invalid") { _, _ -> HTTPResponse.Status in
try await persist.set(key: "test", value: TestCodable(buffer: "hello"))
return .ok
}
router.get("/invalid") { _, _ -> String? in
do {
return try await persist.get(key: "test", as: String.self)
} catch let error as PersistError where error == .invalidConversion {
throw HTTPError(.badRequest)
}
}
}
try await app.test(.router) { client in
try await client.execute(uri: "/invalid", method: .put)
try await client.execute(uri: "/invalid", method: .get) { response in
XCTAssertEqual(response.status, .badRequest)
}
}
}

func testRemove() async throws {
let app = try self.createApplication()
try await app.test(.router) { client in
Expand Down