Skip to content

Commit

Permalink
Improve arg/option error messages (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaoShizhong authored Oct 7, 2024
1 parent d19eaa1 commit e206fd5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,17 @@ function serialize(name, val, opt) {
var enc = (opt && opt.encode) || encodeURIComponent;

if (typeof enc !== 'function') {
throw new TypeError('option encode is invalid');
throw new TypeError('option encode is invalid: ' + enc);
}

if (!cookieNameRegExp.test(name)) {
throw new TypeError('argument name is invalid');
throw new TypeError('argument name is invalid: ' + name);
}

var value = enc(val);

if (!cookieValueRegExp.test(value)) {
throw new TypeError('argument val is invalid');
throw new TypeError('argument val is invalid: ' + value);
}

var str = name + '=' + value;
Expand All @@ -206,23 +206,23 @@ function serialize(name, val, opt) {
var maxAge = Math.floor(opt.maxAge);

if (!isFinite(maxAge)) {
throw new TypeError('option maxAge is invalid')
throw new TypeError('option maxAge is invalid: ' + opt.maxAge)
}

str += '; Max-Age=' + maxAge;
}

if (opt.domain) {
if (!domainValueRegExp.test(opt.domain)) {
throw new TypeError('option domain is invalid');
throw new TypeError('option domain is invalid: ' + opt.domain);
}

str += '; Domain=' + opt.domain;
}

if (opt.path) {
if (!pathValueRegExp.test(opt.path)) {
throw new TypeError('option path is invalid');
throw new TypeError('option path is invalid: ' + opt.path);
}

str += '; Path=' + opt.path;
Expand All @@ -232,7 +232,7 @@ function serialize(name, val, opt) {
var expires = opt.expires

if (!isDate(expires) || isNaN(expires.valueOf())) {
throw new TypeError('option expires is invalid');
throw new TypeError('option expires is invalid: ' + expires);
}

str += '; Expires=' + expires.toUTCString()
Expand Down Expand Up @@ -265,7 +265,7 @@ function serialize(name, val, opt) {
str += '; Priority=High'
break
default:
throw new TypeError('option priority is invalid')
throw new TypeError('option priority is invalid: ' + priority)
}
}

Expand All @@ -287,7 +287,7 @@ function serialize(name, val, opt) {
str += '; SameSite=None';
break;
default:
throw new TypeError('option sameSite is invalid');
throw new TypeError('option sameSite is invalid: ' + sameSite);
}
}

Expand Down

0 comments on commit e206fd5

Please sign in to comment.