Skip to content

Commit

Permalink
feat: add CookieError (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuezier committed May 2, 2022
1 parent 9e770ee commit 7377d3b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.d.ts
Expand Up @@ -58,6 +58,8 @@ declare namespace EggCookies {
*/
signed?: boolean;
}

class CookieError extends Error { }
}

declare class EggCookies {
Expand Down
4 changes: 3 additions & 1 deletion lib/cookies.js
Expand Up @@ -5,6 +5,7 @@ const utility = require('utility');
const _isSameSiteNoneCompatible = require('should-send-same-site-none').isSameSiteNoneCompatible;
const Keygrip = require('./keygrip');
const Cookie = require('./cookie');
const CookieError = require('./error');

const KEYS_ARRAY = Symbol('eggCookies:keysArray');
const KEYS = Symbol('eggCookies:keys');
Expand Down Expand Up @@ -94,7 +95,7 @@ class Cookies {
const signed = computeSigned(opts);
value = value || '';
if (!this.secure && opts.secure) {
throw new Error('Cannot send secure cookie over unencrypted connection');
throw new CookieError('Cannot send secure cookie over unencrypted connection');
}

let headers = this.ctx.response.get('set-cookie') || [];
Expand Down Expand Up @@ -182,4 +183,5 @@ function pushCookie(cookies, cookie) {
return cookies;
}

Cookies.CookieError = CookieError;
module.exports = Cookies;
3 changes: 3 additions & 0 deletions lib/error.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = class CookieError extends Error { };
2 changes: 2 additions & 0 deletions test/lib/cookies.test.js
Expand Up @@ -2,6 +2,7 @@

const Cookies = require('../cookies');
const assert = require('assert');
const CookieError = require('../../lib/error');

describe('test/lib/cookies.test.js', () => {
it('should encrypt error when keys not present', () => {
Expand Down Expand Up @@ -181,6 +182,7 @@ describe('test/lib/cookies.test.js', () => {
cookies.set('foo', 'bar', { secure: true });
throw new Error('should not exec');
} catch (err) {
assert(err instanceof CookieError);
assert(err.message === 'Cannot send secure cookie over unencrypted connection');
}
});
Expand Down

0 comments on commit 7377d3b

Please sign in to comment.