Skip to content

Commit

Permalink
refactoring port-related error message
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 16, 2019
1 parent fc6feed commit 7245295
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function parseHost(host: string, direct?: boolean): IParsedHost | null {
if (port > 0 && port < 65536 && port.toString() === p) {
h.port = port;
} else {
throw new Error('Invalid port number: ' + JSON.stringify(p));
throw new Error('Invalid port: ' + JSON.stringify(p) + '. Valid port range is: [1...65535]');
}
}
if (h.name || h.port) {
Expand Down
14 changes: 8 additions & 6 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function create(defaults: IConnectionDefaults): string {
return (new ConnectionString('', defaults)).toString();
}

const portErrMsg = (txt: string) => 'Invalid port: "' + txt + '". Valid port range is: [1...65535]';

describe('constructor', () => {
it('must throw when used as a function', () => {
expect(() => {
Expand Down Expand Up @@ -184,13 +186,13 @@ describe('hosts', () => {
it('must throw on invalid ports', () => {
expect(() => {
parse(':bad');
}).to.throw('Invalid port number: "bad"');
}).to.throw(portErrMsg('bad'));
expect(() => {
parse('[::]:1a');
}).to.throw('Invalid port number: "1a"');
}).to.throw(portErrMsg('1a'));
expect(() => {
parse('[::]:abc');
}).to.throw('Invalid port number: "abc"');
}).to.throw(portErrMsg('abc'));
});
it('must allow valid ports', () => {
expect(parse('[::]:1')).to.eql({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]});
Expand All @@ -213,15 +215,15 @@ describe('port', () => {
it('must not allow 0 or negative ports', () => {
expect(() => {
parse(':0');
}).to.throw('Invalid port number: "0"');
}).to.throw(portErrMsg('0'));
expect(() => {
parse(':-1');
}).to.throw('Invalid port number: "-1"');
}).to.throw(portErrMsg('-1'));
});
it('must not allow invalid terminators', () => {
expect(() => {
parse(':12345a');
}).to.throw('Invalid port number: "12345a"');
}).to.throw(portErrMsg('12345a'));
});
it('must allow simplified access to the first port number', () => {
expect(parse('').port).to.be.undefined;
Expand Down

0 comments on commit 7245295

Please sign in to comment.