Skip to content

Commit

Permalink
fix: catch errors from zwave driver contructor #61 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Dec 14, 2020
1 parent 36638d2 commit 80093c5
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 80093c5

Please sign in to comment.