diff --git a/desktop/flipper-server-core/src/app-connectivity/ServerRSocket.tsx b/desktop/flipper-server-core/src/app-connectivity/ServerRSocket.tsx index 59db23943b5..84515b68d88 100644 --- a/desktop/flipper-server-core/src/app-connectivity/ServerRSocket.tsx +++ b/desktop/flipper-server-core/src/app-connectivity/ServerRSocket.tsx @@ -47,37 +47,41 @@ class ServerRSocket extends ServerWebSocketBase { start(port: number, sslConfig?: SecureServerConfig): Promise { const self = this; return new Promise((resolve, reject) => { - // eslint-disable-next-line prefer-const - let rawServer: RSocketServer | undefined; - const serverFactory = (onConnect: (socket: Socket) => void) => { - const transportServer = sslConfig - ? tls.createServer(sslConfig, (socket) => { - onConnect(socket); - }) - : net.createServer(onConnect); - transportServer.on('error', reject).on('listening', () => { - console.debug( - `${ - sslConfig ? 'Secure' : 'Certificate' - } server started on port ${port}`, - 'server', - ); - self.listener.onListening(port); - self.rawServer_ = rawServer; - resolve((transportServer.address() as AddressInfo).port); + try { + // eslint-disable-next-line prefer-const + let rawServer: RSocketServer | undefined; + const serverFactory = (onConnect: (socket: Socket) => void) => { + const transportServer = sslConfig + ? tls.createServer(sslConfig, (socket) => { + onConnect(socket); + }) + : net.createServer(onConnect); + transportServer.on('error', reject).on('listening', () => { + console.debug( + `${ + sslConfig ? 'Secure' : 'Certificate' + } server started on port ${port}`, + 'server', + ); + self.listener.onListening(port); + self.rawServer_ = rawServer; + resolve((transportServer.address() as AddressInfo).port); + }); + return transportServer; + }; + rawServer = new RSocketServer({ + getRequestHandler: sslConfig + ? this._trustedRequestHandler + : this._untrustedRequestHandler, + transport: new RSocketTCPServer({ + port: port, + serverFactory: serverFactory, + }), }); - return transportServer; - }; - rawServer = new RSocketServer({ - getRequestHandler: sslConfig - ? this._trustedRequestHandler - : this._untrustedRequestHandler, - transport: new RSocketTCPServer({ - port: port, - serverFactory: serverFactory, - }), - }); - rawServer.start(); + rawServer.start(); + } catch (e) { + reject(e); + } }); } diff --git a/desktop/flipper-server-core/src/app-connectivity/ServerWebSocket.tsx b/desktop/flipper-server-core/src/app-connectivity/ServerWebSocket.tsx index b380ff97abe..0e882e26d15 100644 --- a/desktop/flipper-server-core/src/app-connectivity/ServerWebSocket.tsx +++ b/desktop/flipper-server-core/src/app-connectivity/ServerWebSocket.tsx @@ -49,46 +49,50 @@ class ServerWebSocket extends ServerWebSocketBase { async start(port: number, sslConfig?: SecureServerConfig): Promise { const assignedPort = await new Promise((resolve, reject) => { - const server = sslConfig - ? createHttpsServer(sslConfig) - : createHttpServer(); - - const wsServer = new WSServer({ - server, - verifyClient: this.verifyClient(), - maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE, - }); - - // We do not need to listen to http server's `error` because it is propagated to WS - // https://github.com/websockets/ws/blob/a3a22e4ed39c1a3be8e727e9c630dd440edc61dd/lib/websocket-server.js#L109 - const onConnectionError = (error: Error) => { - reject( - new Error( - `Unable to start server at port ${port} due to ${JSON.stringify( - serializeError(error), - )}`, - ), - ); - }; - wsServer.once('error', onConnectionError); - server.listen(port, () => { - console.debug( - `[ws] ${ - sslConfig ? 'Secure' : 'Insecure' - } server started on port ${port}`, - 'server', - ); - - // Unsubscribe connection error listener. - // We'll attach a permanent error listener later. - wsServer.off('error', onConnectionError); - - this.listener.onListening(port); - this.wsServer = wsServer; - this.httpServer = server; + try { + const server = sslConfig + ? createHttpsServer(sslConfig) + : createHttpServer(); + + const wsServer = new WSServer({ + server, + verifyClient: this.verifyClient(), + maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE, + }); - resolve((server.address() as AddressInfo).port); - }); + // We do not need to listen to http server's `error` because it is propagated to WS + // https://github.com/websockets/ws/blob/a3a22e4ed39c1a3be8e727e9c630dd440edc61dd/lib/websocket-server.js#L109 + const onConnectionError = (error: Error) => { + reject( + new Error( + `Unable to start server at port ${port} due to ${JSON.stringify( + serializeError(error), + )}`, + ), + ); + }; + wsServer.once('error', onConnectionError); + server.listen(port, () => { + console.debug( + `[ws] ${ + sslConfig ? 'Secure' : 'Insecure' + } server started on port ${port}`, + 'server', + ); + + // Unsubscribe connection error listener. + // We'll attach a permanent error listener later. + wsServer.off('error', onConnectionError); + + this.listener.onListening(port); + this.wsServer = wsServer; + this.httpServer = server; + + resolve((server.address() as AddressInfo).port); + }); + } catch (e) { + reject(e); + } }); assertNotNull(this.wsServer);