diff --git a/lib/ZwaveClient.js b/lib/ZwaveClient.js index b472dca20b0..e4b2ab9e614 100644 --- a/lib/ZwaveClient.js +++ b/lib/ZwaveClient.js @@ -1296,26 +1296,30 @@ ZwaveClient.prototype.connect = async function () { networkKey = undefined } - // init driver here because if connect fails the driver is destroyed - this.driver = new Driver(this.cfg.port, { - cacheDir: storeDir, - networkKey: networkKey - }) - this.driver.on('error', driverError.bind(this)) - this.driver.once('driver ready', driverReady.bind(this)) - this.driver.on('all nodes ready', scanComplete.bind(this)) + try { + // init driver here because if connect fails the driver is destroyed + // this could throw so include in the try/catch + this.driver = new Driver(this.cfg.port, { + cacheDir: storeDir, + networkKey: networkKey + }) - debug('Connecting to', this.cfg.port) + this.driver.on('error', driverError.bind(this)) + this.driver.once('driver ready', driverReady.bind(this)) + this.driver.on('all nodes ready', scanComplete.bind(this)) + + debug('Connecting to', this.cfg.port) - try { await this.driver.start() this.status = ZWAVE_STATUS.connected this.connected = true } catch (error) { // destroy diver instance when it fails - this.driver.destroy().catch(err => { - debug('Error while destroing driver ' + err.message) - }) + if (this.driver) { + this.driver.destroy().catch(err => { + debug('Error while destroing driver ' + err.message) + }) + } driverError.call(this, error) debug('Retry connection in 3 seconds...') this.reconnectTimeout = setTimeout(this.connect.bind(this), 3000)