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

fix: catch errors from zwave driver contructor #61 #75

Merged
merged 1 commit into from
Dec 14, 2020
Merged
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
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