Skip to content

Commit 6634e09

Browse files
committed
Return callback error vs throwing
Signed-off-by: Matthew Peveler <[email protected]>
1 parent 0f01a6f commit 6634e09

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/portfinder.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ internals.getPort = function (options, callback) {
128128
options.host = options.host || null;
129129
options.stopPort = Number(options.stopPort) || Number(exports.highestPort);
130130

131-
if(!options.startPort) {
132-
options.startPort = Number(options.port);
133-
if(options.startPort < 0) {
134-
throw Error('Provided options.startPort(' + options.startPort + ') is less than 0, which are cannot be bound.');
131+
if (!options.startPort) {
132+
options.startPort = options.port;
133+
if (options.startPort < 0) {
134+
return callback(Error(`Provided options.port(${options.port}) is less than 0, which are cannot be bound.`));
135135
}
136-
if(options.stopPort < options.startPort) {
137-
throw Error('Provided options.stopPort(' + options.stopPort + ') is less than options.startPort (' + options.startPort + ')');
136+
if (options.stopPort < options.startPort) {
137+
return callback(Error(`Provided options.stopPort(${options.stopPort}) is less than options.port(${options.startPort})`));
138138
}
139139
}
140140

test/port-finder.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,21 @@ describe('with no available ports above the start port', function () {
227227
});
228228
});
229229
});
230+
231+
test('should return error if port is negative', function (done) {
232+
portfinder.getPort({ port: -1 }, function (err, port) {
233+
expect(err).not.toBeNull();
234+
expect(err.message).toEqual('Provided options.port(-1) is less than 0, which are cannot be bound.');
235+
expect(port).toBeUndefined();
236+
done();
237+
});
238+
});
239+
240+
test('should return error if port is less than stopPort', function (done) {
241+
portfinder.getPort({ port: 32768, stopPort: 32767 }, function (err, port) {
242+
expect(err).not.toBeNull();
243+
expect(err.message).toEqual('Provided options.stopPort(32767) is less than options.port(32768)');
244+
expect(port).toBeUndefined();
245+
done();
246+
});
247+
});

0 commit comments

Comments
 (0)