File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,7 +27,11 @@ class Client {
2727
2828 static async getInstance ( req , res ) {
2929 const client = new Client ( req , res ) ;
30- await Session . restore ( client ) ;
30+ try {
31+ await Session . restore ( client ) ;
32+ } catch {
33+ throw new Error ( "Couldn't restore session" ) ;
34+ }
3135 return client ;
3236 }
3337
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ const http = require('node:http');
55const Client = require ( './client.js' ) ;
66const Session = require ( './session.js' ) ;
77
8+ const INVALID_TOKEN_CODE = 498 ;
9+
810const routing = {
911 '/' : async ( ) => '<h1>welcome to homepage</h1><hr>' ,
1012 '/start' : async ( client ) => {
@@ -46,7 +48,13 @@ const types = {
4648} ;
4749
4850http . createServer ( async ( req , res ) => {
49- const client = await Client . getInstance ( req , res ) ;
51+ try {
52+ const client = await Client . getInstance ( req , res ) ;
53+ } catch {
54+ res . statusCode = INVALID_TOKEN_CODE ;
55+ res . end ( ) ;
56+ return ;
57+ }
5058 const { method, url, headers } = req ;
5159 console . log ( `${ method } ${ url } ${ headers . cookie } ` ) ;
5260 const handler = routing [ url ] ;
Original file line number Diff line number Diff line change @@ -43,7 +43,10 @@ class Session extends Map {
4343 if ( sessionToken ) {
4444 return new Promise ( ( resolve , reject ) => {
4545 storage . get ( sessionToken , ( err , session ) => {
46- if ( err ) reject ( new Error ( 'No session' ) ) ;
46+ if ( err ) {
47+ reject ( new Error ( 'No session' ) ) ;
48+ return ;
49+ }
4750 Object . setPrototypeOf ( session , Session . prototype ) ;
4851 client . token = sessionToken ;
4952 client . session = session ;
You can’t perform that action at this time.
0 commit comments