Skip to content

Commit c9c8542

Browse files
lutastimonreinhard
authored andcommitted
Add callback if an error occurs during Wemo.load (#51)
1 parent f21ed18 commit c9c8542

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ The callback will only be called for newly found devices (those that have not be
9797

9898
#### load(setupUrl, cb)
9999

100-
Allows to skip discovery if the `setupUrl` of a Wemo is already known. A `deviceInfo` will be passed to `cb` that can be used to get a client for the device found.
100+
Allows to skip discovery if the `setupUrl` of a Wemo is already known. A `deviceInfo` will be passed to `cb` that can be used to get a client for the device found. The `err` field will be non-null in the event of an error.
101101

102102
* **String** *setupUrl* Must point to setup.xml of the requested device (`http://device_ip:device_port/setup.xml`).
103-
* **Callback** *cb*
103+
* **Callback** *cb* cb(err, deviceInfo)
104104

105105
#### client(deviceInfo)
106106

index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ Wemo.prototype.load = function(setupUrl, cb) {
4545
if (!self._clients[device.UDN] || self._clients[device.UDN].error) {
4646
debug('Found device: %j', json);
4747
if (cb) {
48-
cb.call(self, device);
48+
cb.call(self, err, device);
4949
}
5050
}
51+
} else {
52+
debug('Error occurred connecting to device at %s', location);
53+
if (cb) {
54+
cb.call(self, err, null);
55+
}
5156
}
5257
});
5358
};

test/index.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Wemo', function() {
3838
req.end();
3939
});
4040

41-
describe('#load(setupUrl, cb)', function() {
41+
describe('#load(setupUrl, cb, err)', function() {
4242
it('must load a device', function(done) {
4343
var wemo = new Wemo();
4444
var mitm = Mitm();
@@ -50,12 +50,30 @@ describe('Wemo', function() {
5050
mitm.disable();
5151
});
5252

53-
wemo.load('http://127.0.0.2/setup.xml', function(device) {
54-
deviceInfo.serialNumber.must.be('000000000000B');
53+
wemo.load('http://127.0.0.2/setup.xml', function(err, device) {
54+
demand(err).must.be.falsy();
55+
device.serialNumber.must.be('000000000000B');
5556
done();
5657
});
5758

5859
});
60+
61+
it('must error if no connection can be established', function(done) {
62+
var wemo = new Wemo();
63+
var mitm = Mitm();
64+
mitm.on('request', function(req, res) {
65+
req.url.must.be('/setup.xml');
66+
res.statusCode = 404;
67+
res.write('Failed to connect');
68+
res.end();
69+
mitm.disable();
70+
});
71+
72+
wemo.load('http://127.0.0.2/setup.xml', function(err) {
73+
demand(err).not.be.undefined();
74+
done();
75+
});
76+
});
5977
});
6078

6179
});

0 commit comments

Comments
 (0)