Skip to content
This repository was archived by the owner on Jun 4, 2023. It is now read-only.

Commit b08132c

Browse files
committed
add error handling for individual devices, prevent crash if one device throws error
* individual setups should run in a domain, so we can catch errors individually * add feature to README.md
1 parent f90c76d commit b08132c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ When this option is set, airsonos skips teh autodiscovery part and tries to conn
3434
specified in <device_list>. A Sonos device is specified by its IP.
3535
Optionally you can set also its TCP port if the device is set to not use the default port (1400).
3636

37+
As long as airsonos is able to connect to at least one device, it will not end itself.
38+
3739
This is the syntax:
3840
```
3941
airsonos --devices SONOS1_IP[:SONOS1_PORT][,SONOS2_IP[:SONOS2_PORT][,...]]

lib/main.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,35 @@ if (flags.get('version')) {
110110
diag();
111111

112112
} else if (flags.get('devices').length > 0) {
113-
var useDevices = flags.get('devices');
114-
for (var i = 0; i < useDevices.length; i++) {
115-
var ipPortSplit = useDevices[i].split(':');
113+
114+
var deviceArguments = flags.get('devices');
115+
for(var i = 0; i < deviceArguments.length; i++) {
116+
117+
var deviceArg = deviceArguments[i];
118+
119+
// set up a domain to catch errors from setting up individual
120+
// devices. For example it catches the timeout when non-existing IPs
121+
// are entered.
122+
var setupDomain = require('domain').create();
123+
setupDomain.on('error', function(err) {
124+
console.error('Error on setting up device "' + deviceArg + '": ' + err);
125+
});
126+
116127
var sonosDevice;
128+
129+
// Check if the argument contains a port specifier
130+
var ipPortSplit = deviceArg.split(':');
117131
if (ipPortSplit.length > 1) {
118132
sonosDevice = new sonos.Sonos(ipPortSplit[0], ipPortSplit[1]);
119133
} else {
120-
sonosDevice = new sonos.Sonos(useDevices[i]);
134+
sonosDevice = new sonos.Sonos(deviceArg);
121135
}
122136

123-
setupSonos(sonosDevice, 'Sonos' + i);
137+
// run the setup in a domain so we can catch errors for each device
138+
// individually:
139+
setupDomain.run(function() {
140+
setupSonos(sonosDevice);
141+
});
124142
}
125143

126144
} else {

0 commit comments

Comments
 (0)