diff --git a/lib/ping-promise.js b/lib/ping-promise.js index dfa93ad..e96afdd 100644 --- a/lib/ping-promise.js +++ b/lib/ping-promise.js @@ -25,12 +25,9 @@ var builderFactory = require('./builder/factory'); var parserFactory = require('./parser/factory'); /** - * Class::PromisePing - * @param {string} addr - Hostname or ip addres - * @param {PingConfig} config - Configuration for command ping - * @return {Promise} + * Refer to probe() */ -function probe(addr, config) { +function _probe(addr, config) { // Do not reassign function argument var _config = config || {}; if (_config.v6 === undefined) { @@ -94,4 +91,21 @@ function probe(addr, config) { return deferred.promise; } + +/** + * Class::PromisePing + * @param {string} addr - Hostname or ip addres + * @param {PingConfig} config - Configuration for command ping + * @return {Promise} + */ +function probe(addr, config) { + try { + var probePromise = _probe(addr, config); + return probePromise; + } catch (error) { + var errorPromise = Q.reject(error); + return errorPromise; + } +} + exports.probe = probe; diff --git a/test/test-ping.js b/test/test-ping.js index b539caf..30b6055 100644 --- a/test/test-ping.js +++ b/test/test-ping.js @@ -303,3 +303,29 @@ describe('Ping ipv6 on MAC OS', function () { }); }); }); + +describe('Ping in promise mode with unknown exception', function () { + var pingExecution = function (fp, args) { + var unknownException = new Error('Unknown error!'); + var stub = sinon.stub(cp, 'spawn').throws(unknownException); + + var ret = null; + var _args = args; + if (fp.includes('v6')) { + _args = _args || {}; + _args.v6 = true; + } + ret = ping.promise.probe('whatever', _args); + + stub.restore(); + + return ret.catch(function (err) { + expect(err.message).to.be.a('string'); + expect(err.message).to.include('Unknown error!'); + }); + }; + + PLATFORMS.forEach(function (platform) { + createTestCase(platform, pingExecution); + }); +});