From 7245295ffa6ecae90f87e5a38fd8156f8dee2992 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 16 Nov 2019 14:49:40 +0000 Subject: [PATCH] refactoring port-related error message --- src/static.ts | 2 +- test/main.spec.ts | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/static.ts b/src/static.ts index 47b4f27..c35a469 100644 --- a/src/static.ts +++ b/src/static.ts @@ -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) { diff --git a/test/main.spec.ts b/test/main.spec.ts index f6417b4..1710ead 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -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(() => { @@ -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'}]}); @@ -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;