Skip to content

Commit

Permalink
Wrap exceptions for running probe as a promise
Browse files Browse the repository at this point in the history
refs #122
  • Loading branch information
Mond Wan committed Dec 19, 2020
1 parent 3b441a7 commit 1ebe7c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/ping-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
26 changes: 26 additions & 0 deletions test/test-ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 1ebe7c0

Please sign in to comment.