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

WebSocket Client should throw errors it catches from handler #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion Sources/HummingbirdWSCore/WebSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ package actor WebSocketHandler {
}
}
}
let rt = try await webSocketHandler.handle(inbound: inbound, outbound: outbound, handler: handler, context: context)
let rt = try await webSocketHandler.handle(type: type, inbound: inbound, outbound: outbound, handler: handler, context: context)
group.cancelAll()
return rt
}
Expand All @@ -153,6 +153,7 @@ package actor WebSocketHandler {
}

func handle<Context: WebSocketContext>(
type: WebSocketType,
inbound: NIOAsyncChannelInboundStream<WebSocketFrame>,
outbound: NIOAsyncChannelOutboundWriter<WebSocketFrame>,
handler: @escaping WebSocketDataHandler<Context>,
Expand All @@ -166,13 +167,15 @@ package actor WebSocketHandler {
handler: self
)
let closeCode: WebSocketErrorCode
var clientError: Error?
do {
// handle websocket data and text
try await handler(webSocketInbound, webSocketOutbound, context)
closeCode = .normalClosure
} catch InternalError.close(let code) {
closeCode = code
} catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch {
} catch where type == .server {

Can't we do something like this instead?

clientError = error
closeCode = .unexpectedServerError
}
do {
Expand All @@ -188,6 +191,9 @@ package actor WebSocketHandler {
}
// don't propagate error if channel is already closed
} catch ChannelError.ioOnClosedChannel {}
if type == .client, let clientError {
throw clientError
}
} onGracefulShutdown: {
Task {
try? await self.close(code: .normalClosure)
Expand Down
50 changes: 39 additions & 11 deletions Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,17 +708,45 @@ final class HummingbirdWebSocketTests: XCTestCase {
await serviceGroup.triggerGracefulShutdown()
}
}
}

extension Logger {
/// Create new Logger with additional metadata value
/// - Parameters:
/// - metadataKey: Metadata key
/// - value: Metadata value
/// - Returns: Logger
func with(metadataKey: String, value: MetadataValue) -> Logger {
var logger = self
logger[metadataKey: metadataKey] = value
return logger
func testClientErrorHandling() async throws {
struct ClientError: Error {}
let app = Application(
router: Router(),
server: .http1WebSocketUpgrade { _, _, _ in
.upgrade([:]) { inbound, _, _ in
for try await _ in inbound {}
}
}
)
try await app.test(.live) { client in
do {
_ = try await client.ws("/") { _, _, _ in
throw ClientError()
}
XCTFail("Shouldnt reach here")
} catch is ClientError {
} catch {
XCTFail("Throwing wrong error")
}
}
}

func testServerErrorHandling() async throws {
struct ServerError: Error {}
let app = Application(
router: Router(),
server: .http1WebSocketUpgrade { _, _, _ in
.upgrade([:]) { _, _, _ in
throw ServerError()
}
}
)
try await app.test(.live) { client in
let closeFrame = try await client.ws("/") { inbound, _, _ in
for try await _ in inbound {}
}
XCTAssertEqual(closeFrame?.closeCode, .unexpectedServerError)
}
}
}
Loading