@@ -363,6 +363,19 @@ function session(options) {
363363 wrapmethods ( req . session ) ;
364364 }
365365
366+ // inflate the session
367+ function inflate ( req , sess ) {
368+ store . createSession ( req , sess )
369+ originalId = req . sessionID
370+ originalHash = hash ( sess )
371+
372+ if ( ! resaveSession ) {
373+ savedHash = originalHash
374+ }
375+
376+ wrapmethods ( req . session )
377+ }
378+
366379 // wrap session methods
367380 function wrapmethods ( sess ) {
368381 var _reload = sess . reload
@@ -460,34 +473,26 @@ function session(options) {
460473 debug ( 'fetching %s' , req . sessionID ) ;
461474 store . get ( req . sessionID , function ( err , sess ) {
462475 // error handling
463- if ( err ) {
476+ if ( err && err . code !== 'ENOENT' ) {
464477 debug ( 'error %j' , err ) ;
478+ next ( err )
479+ return
480+ }
465481
466- if ( err . code !== 'ENOENT' ) {
467- next ( err ) ;
468- return ;
469- }
470-
471- generate ( ) ;
472- // no session
473- } else if ( ! sess ) {
474- debug ( 'no session found' ) ;
475- generate ( ) ;
476- // populate req.session
477- } else {
478- debug ( 'session found' ) ;
479- store . createSession ( req , sess ) ;
480- originalId = req . sessionID ;
481- originalHash = hash ( sess ) ;
482-
483- if ( ! resaveSession ) {
484- savedHash = originalHash
482+ try {
483+ if ( err || ! sess ) {
484+ debug ( 'no session found' )
485+ generate ( )
486+ } else {
487+ debug ( 'session found' )
488+ inflate ( req , sess )
485489 }
486-
487- wrapmethods ( req . session ) ;
490+ } catch ( e ) {
491+ next ( e )
492+ return
488493 }
489494
490- next ( ) ;
495+ next ( )
491496 } ) ;
492497 } ;
493498} ;
0 commit comments