Skip to content

Commit

Permalink
Fix handling errors from setting cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 28, 2024
1 parent f9f2318 commit d9354ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Add `priority` to `cookie` options
* Fix handling errors from setting cookie
* Support any type in `secret` that `crypto.createHmac` supports
* deps: cookie@0.5.0
- Fix `expires` option to reject invalid dates
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ function session(options) {
}

// set cookie
setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data);
try {
setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data)
} catch (err) {
defer(next, err)
}
});

// proxy end() to commit the session
Expand Down
17 changes: 17 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,23 @@ describe('session()', function(){
.expect(200, done)
})

it('should forward errors setting cookie', function (done) {
var cb = after(2, done)
var server = createServer({ cookie: { expires: new Date(NaN) } }, function (req, res) {
res.end()
})

server.on('error', function onerror (err) {
assert.ok(err)
assert.strictEqual(err.message, 'option expires is invalid')
cb()
})

request(server)
.get('/admin')
.expect(200, cb)
})

it('should preserve cookies set before writeHead is called', function(done){
var server = createServer(null, function (req, res) {
var cookie = new Cookie()
Expand Down

0 comments on commit d9354ef

Please sign in to comment.