diff --git a/Sources/WebSocketKit/WebSocketClient.swift b/Sources/WebSocketKit/WebSocketClient.swift index 6ee4cebf..1eb4df78 100644 --- a/Sources/WebSocketKit/WebSocketClient.swift +++ b/Sources/WebSocketKit/WebSocketClient.swift @@ -91,14 +91,18 @@ public final class WebSocketClient { ) if scheme == "wss" { - let context = try! NIOSSLContext( - configuration: self.configuration.tlsConfiguration ?? .forClient() - ) - let tlsHandler = try! NIOSSLClientHandler(context: context, serverHostname: host) - return channel.pipeline.addHandler(tlsHandler).flatMap { - channel.pipeline.addHTTPClientHandlers(leftOverBytesStrategy: .forwardBytes, withClientUpgrade: config) - }.flatMap { - channel.pipeline.addHandler(httpHandler) + do { + let context = try NIOSSLContext( + configuration: self.configuration.tlsConfiguration ?? .forClient() + ) + let tlsHandler = try NIOSSLClientHandler(context: context, serverHostname: host) + return channel.pipeline.addHandler(tlsHandler).flatMap { + channel.pipeline.addHTTPClientHandlers(leftOverBytesStrategy: .forwardBytes, withClientUpgrade: config) + }.flatMap { + channel.pipeline.addHandler(httpHandler) + } + } catch { + return channel.pipeline.close(mode: .all) } } else { return channel.pipeline.addHTTPClientHandlers(