From b382bbe1d08d65ec8b17f97e81e86ca0ccea314a Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Thu, 2 May 2024 14:52:25 +0100 Subject: [PATCH 1/6] Improve error message descriptiveness --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7506a79..7dd0926 100644 --- a/index.js +++ b/index.js @@ -113,7 +113,7 @@ function serialize(name, val, options) { var enc = opt.encode || encode; if (typeof enc !== 'function') { - throw new TypeError('option encode is invalid'); + throw new TypeError('the encode option expected a function but received ' + enc); } if (!fieldContentRegExp.test(name)) { @@ -132,7 +132,7 @@ function serialize(name, val, options) { var maxAge = opt.maxAge - 0; if (isNaN(maxAge) || !isFinite(maxAge)) { - throw new TypeError('option maxAge is invalid') + throw new TypeError(opt.maxAge + ' is not a valid value for the maxAge option') } str += '; Max-Age=' + Math.floor(maxAge); @@ -140,7 +140,7 @@ function serialize(name, val, options) { if (opt.domain) { if (!fieldContentRegExp.test(opt.domain)) { - throw new TypeError('option domain is invalid'); + throw new TypeError(opt.domain + ' is not a valid value for the domain option'); } str += '; Domain=' + opt.domain; @@ -148,7 +148,7 @@ function serialize(name, val, options) { if (opt.path) { if (!fieldContentRegExp.test(opt.path)) { - throw new TypeError('option path is invalid'); + throw new TypeError(opt.path + ' is not a valid value for the path option'); } str += '; Path=' + opt.path; @@ -158,7 +158,7 @@ function serialize(name, val, options) { var expires = opt.expires if (!isDate(expires) || isNaN(expires.valueOf())) { - throw new TypeError('option expires is invalid'); + throw new TypeError(expires + ' is not a valid value for the expires option'); } str += '; Expires=' + expires.toUTCString() @@ -192,7 +192,7 @@ function serialize(name, val, options) { str += '; Priority=High' break default: - throw new TypeError('option priority is invalid') + throw new TypeError(priority + ' is not a valid value for the priority option') } } @@ -214,7 +214,7 @@ function serialize(name, val, options) { str += '; SameSite=None'; break; default: - throw new TypeError('option sameSite is invalid'); + throw new TypeError(sameSite + ' is not a valid value for the sameSite option'); } } From 667ed86ad757c5249d9c63fd5ebc117e81a113c3 Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Thu, 2 May 2024 15:00:07 +0100 Subject: [PATCH 2/6] Amend error-handling tests with new error messages --- test/serialize.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/serialize.js b/test/serialize.js index 80d0c48..55f05a7 100644 --- a/test/serialize.js +++ b/test/serialize.js @@ -32,14 +32,14 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw for invalid value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { domain: 'example.com\n' }), - /option domain is invalid/) + /example.com\n is not a valid value for the domain option/) }) }) describe('with "encode" option', function () { it('should throw on non-function value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { encode: 42 }), - /option encode is invalid/) + /the encode option expected a function but received 42/) }) it('should specify alternative value encoder', function () { @@ -58,12 +58,12 @@ describe('cookie.serialize(name, value, options)', function () { describe('with "expires" option', function () { it('should throw on non-Date value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: 42 }), - /option expires is invalid/) + /42 is not a valid value for the expires option/) }) it('should throw on invalid date', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: new Date(NaN) }), - /option expires is invalid/) + /Invalid Date is not a valid value for the expires option/) }) it('should set expires to given date', function () { @@ -87,13 +87,13 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw when not a number', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: 'buzz' }) - }, /option maxAge is invalid/) + }, /buzz is not a valid value for the maxAge option/) }) it('should throw when Infinity', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: Infinity }) - }, /option maxAge is invalid/) + }, /Infinity is not a valid value for the maxAge option/) }) it('should set max-age to value', function () { @@ -134,7 +134,7 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw for invalid value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { path: '/\n' }), - /option path is invalid/) + /\/\n is not a valid value for the path optio/) }) }) @@ -142,13 +142,13 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw on invalid priority', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 'foo' }) - }, /option priority is invalid/) + }, /foo is not a valid value for the priority optio/) }) it('should throw on non-string', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 42 }) - }, /option priority is invalid/) + }, /42 is not a valid value for the priority option/) }) it('should set priority low', function () { @@ -171,7 +171,7 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw on invalid sameSite', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { sameSite: 'foo' }) - }, /option sameSite is invalid/) + }, /foo is not a valid value for the sameSite option/) }) it('should set sameSite strict', function () { From 304c6ffbad74b3261fb7b83cb3ac058feda5f258 Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Thu, 2 May 2024 15:06:47 +0100 Subject: [PATCH 3/6] Add accidentally omitted letter from test regexes --- test/serialize.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/serialize.js b/test/serialize.js index 55f05a7..c5e962c 100644 --- a/test/serialize.js +++ b/test/serialize.js @@ -134,7 +134,7 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw for invalid value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { path: '/\n' }), - /\/\n is not a valid value for the path optio/) + /\/\n is not a valid value for the path option/) }) }) @@ -142,7 +142,7 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw on invalid priority', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 'foo' }) - }, /foo is not a valid value for the priority optio/) + }, /foo is not a valid value for the priority option/) }) it('should throw on non-string', function () { From 2c6ebbd0e5c90f057f429a8681bd39ec1ad050ef Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 7 Oct 2024 14:06:22 -0700 Subject: [PATCH 4/6] Use old error message format --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 7dd0926..b7e5c54 100644 --- a/index.js +++ b/index.js @@ -113,17 +113,17 @@ function serialize(name, val, options) { var enc = opt.encode || encode; if (typeof enc !== 'function') { - throw new TypeError('the encode option expected a function but received ' + enc); + throw new TypeError('option encode is invalid: ' + enc); } if (!fieldContentRegExp.test(name)) { - throw new TypeError('argument name is invalid'); + throw new TypeError('argument name is invalid: ' + name); } var value = enc(val); if (value && !fieldContentRegExp.test(value)) { - throw new TypeError('argument val is invalid'); + throw new TypeError('argument val is invalid: ' + value); } var str = name + '=' + value; @@ -132,7 +132,7 @@ function serialize(name, val, options) { var maxAge = opt.maxAge - 0; if (isNaN(maxAge) || !isFinite(maxAge)) { - throw new TypeError(opt.maxAge + ' is not a valid value for the maxAge option') + throw new TypeError('option maxAge is invalid: ' + opt.maxAge) } str += '; Max-Age=' + Math.floor(maxAge); @@ -140,7 +140,7 @@ function serialize(name, val, options) { if (opt.domain) { if (!fieldContentRegExp.test(opt.domain)) { - throw new TypeError(opt.domain + ' is not a valid value for the domain option'); + throw new TypeError('option domain is invalid: ' + opt.domain); } str += '; Domain=' + opt.domain; @@ -148,7 +148,7 @@ function serialize(name, val, options) { if (opt.path) { if (!fieldContentRegExp.test(opt.path)) { - throw new TypeError(opt.path + ' is not a valid value for the path option'); + throw new TypeError('option path is invalid: ' + opt.path); } str += '; Path=' + opt.path; @@ -158,7 +158,7 @@ function serialize(name, val, options) { var expires = opt.expires if (!isDate(expires) || isNaN(expires.valueOf())) { - throw new TypeError(expires + ' is not a valid value for the expires option'); + throw new TypeError('option expires is invalid: ' + expires); } str += '; Expires=' + expires.toUTCString() @@ -192,7 +192,7 @@ function serialize(name, val, options) { str += '; Priority=High' break default: - throw new TypeError(priority + ' is not a valid value for the priority option') + throw new TypeError('option priority is invalid: ' + priority) } } @@ -214,7 +214,7 @@ function serialize(name, val, options) { str += '; SameSite=None'; break; default: - throw new TypeError(sameSite + ' is not a valid value for the sameSite option'); + throw new TypeError('option sameSite is invalid: ' + sameSite); } } From dc0323ad9088b6786336bec51dc56f4857af967e Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 7 Oct 2024 14:09:57 -0700 Subject: [PATCH 5/6] Update index.js --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index 9b9c159..56b1196 100644 --- a/index.js +++ b/index.js @@ -189,14 +189,12 @@ function serialize(name, val, opt) { throw new TypeError('option encode is invalid: ' + enc); } - if (!cookieNameRegExp.test(name)) { throw new TypeError('argument name is invalid: ' + name); } var value = enc(val); - if (!cookieValueRegExp.test(value)) { throw new TypeError('argument val is invalid: ' + value); } From 3fcd126fef6b6c4fb8360f2cbbf5d1a54e0a6c15 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 7 Oct 2024 14:11:38 -0700 Subject: [PATCH 6/6] Update serialize.js --- test/serialize.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/serialize.js b/test/serialize.js index e164074..1ee3235 100644 --- a/test/serialize.js +++ b/test/serialize.js @@ -114,7 +114,7 @@ describe('cookie.serialize(name, value, options)', function () { describe('with "encode" option', function () { it('should throw on non-function value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { encode: 42 }), - /the encode option expected a function but received 42/) + /option encode is invalid/) }) it('should specify alternative value encoder', function () { @@ -136,12 +136,12 @@ describe('cookie.serialize(name, value, options)', function () { describe('with "expires" option', function () { it('should throw on non-Date value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: 42 }), - /42 is not a valid value for the expires option/) + /option expires is invalid/) }) it('should throw on invalid date', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: new Date(NaN) }), - /Invalid Date is not a valid value for the expires option/) + /option expires is invalid/) }) it('should set expires to given date', function () { @@ -165,13 +165,13 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw when not a number', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: 'buzz' }) - }, /buzz is not a valid value for the maxAge option/) + }, /option maxAge is invalid/) }) it('should throw when Infinity', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: Infinity }) - }, /Infinity is not a valid value for the maxAge option/) + }, /option maxAge is invalid/) }) it('should set max-age to value', function () { @@ -251,13 +251,13 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw on invalid priority', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 'foo' }) - }, /foo is not a valid value for the priority option/) + }, /option priority is invalid/) }) it('should throw on non-string', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 42 }) - }, /42 is not a valid value for the priority option/) + }, /option priority is invalid/) }) it('should set priority low', function () { @@ -280,7 +280,7 @@ describe('cookie.serialize(name, value, options)', function () { it('should throw on invalid sameSite', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { sameSite: 'foo' }) - }, /foo is not a valid value for the sameSite option/) + }, /option sameSite is invalid/) }) it('should set sameSite strict', function () {