@@ -139,49 +139,56 @@ export async function handleApiRequest(request, env) {
139139
140140 if ( ! initialReq . ok && initialReq . status !== 404 ) {
141141 // eslint-disable-next-line no-console
142- console . log ( `${ initialReq . status } - ${ initialReq . statusText } ` ) ;
142+ console . log ( `Unable to get resource ${ docName } : ${ initialReq . status } - ${ initialReq . statusText } ` ) ;
143143 return new Response ( 'unable to get resource' , { status : initialReq . status } ) ;
144144 }
145145
146146 const daActions = initialReq . headers . get ( 'X-da-actions' ) ?? '' ;
147147 [ , authActions ] = daActions . split ( '=' ) ;
148148 } catch ( err ) {
149149 // eslint-disable-next-line no-console
150- console . log ( err ) ;
150+ console . error ( `Unable to handle API request ${ docName } ` , err ) ;
151151 return new Response ( 'unable to get resource' , { status : 500 } ) ;
152152 }
153153
154- const timingBeforeDocRoomGet = Date . now ( ) ;
155- // Each Durable Object has a 256-bit unique ID. Route the request based on the path.
156- const id = env . rooms . idFromName ( docName ) ;
157-
158- // Get the Durable Object stub for this room! The stub is a client object that can be used
159- // to send messages to the remote Durable Object instance. The stub is returned immediately;
160- // there is no need to await it. This is important because you would not want to wait for
161- // a network round trip before you could start sending requests. Since Durable Objects are
162- // created on-demand when the ID is first used, there's nothing to wait for anyway; we know
163- // an object will be available somewhere to receive our requests.
164- const roomObject = env . rooms . get ( id ) ;
165- const timingDocRoomGetDuration = Date . now ( ) - timingBeforeDocRoomGet ;
154+ try {
155+ const timingBeforeDocRoomGet = Date . now ( ) ;
156+ // Each Durable Object has a 256-bit unique ID. Route the request based on the path.
157+ const id = env . rooms . idFromName ( docName ) ;
158+
159+ // Get the Durable Object stub for this room! The stub is a client object that can be used
160+ // to send messages to the remote Durable Object instance. The stub is returned immediately;
161+ // there is no need to await it. This is important because you would not want to wait for
162+ // a network round trip before you could start sending requests. Since Durable Objects are
163+ // created on-demand when the ID is first used, there's nothing to wait for anyway; we know
164+ // an object will be available somewhere to receive our requests.
165+ const roomObject = env . rooms . get ( id ) ;
166+ const timingDocRoomGetDuration = Date . now ( ) - timingBeforeDocRoomGet ;
166167
167- // eslint-disable-next-line no-console
168- console . log ( `FETCHING: ${ docName } ${ id } ` ) ;
169-
170- const headers = [ ...request . headers ,
171- [ 'X-collab-room' , docName ] ,
172- [ 'X-timing-start' , timingStartTime ] ,
173- [ 'X-timing-da-admin-head-duration' , timingDaAdminHeadDuration ] ,
174- [ 'X-timing-docroom-get-duration' , timingDocRoomGetDuration ] ,
175- [ 'X-auth-actions' , authActions ] ,
176- ] ;
177- if ( auth ) {
178- headers . push ( [ 'Authorization' , auth ] ) ;
168+ // eslint-disable-next-line no-console
169+ console . log ( `Fetching: ${ docName } ${ id } ` ) ;
170+
171+ const headers = [ ...request . headers ,
172+ [ 'X-collab-room' , docName ] ,
173+ [ 'X-timing-start' , timingStartTime ] ,
174+ [ 'X-timing-da-admin-head-duration' , timingDaAdminHeadDuration ] ,
175+ [ 'X-timing-docroom-get-duration' , timingDocRoomGetDuration ] ,
176+ [ 'X-auth-actions' , authActions ] ,
177+ ] ;
178+ if ( auth ) {
179+ headers . push ( [ 'Authorization' , auth ] ) ;
180+ }
181+ const req = new Request ( new URL ( docName ) , { headers } ) ;
182+ // Send the request to the Durable Object. The `fetch()` method of a Durable Object stub has the
183+ // same signature as the global `fetch()` function, but the request is always sent to the
184+ // object, regardless of the hostname in the request's URL.
185+ const res = await roomObject . fetch ( req ) ;
186+ return res ;
187+ } catch ( err ) {
188+ // eslint-disable-next-line no-console
189+ console . error ( `Error fetching the doc from the room ${ docName } ` , err ) ;
190+ return new Response ( 'unable to get resource' , { status : 500 } ) ;
179191 }
180- const req = new Request ( new URL ( docName ) , { headers } ) ;
181- // Send the request to the Durable Object. The `fetch()` method of a Durable Object stub has the
182- // same signature as the global `fetch()` function, but the request is always sent to the
183- // object, regardless of the hostname in the request's URL.
184- return roomObject . fetch ( req ) ;
185192}
186193
187194// In modules-syntax workers, we use `export default` to export our script's main event handlers.
@@ -311,7 +318,7 @@ export class DocRoom {
311318 webSocket . readOnly = true ;
312319 }
313320 // eslint-disable-next-line no-console
314- console . log ( `setupWSConnection ${ docName } with auth(${ webSocket . auth
321+ console . log ( `Setting up WSConnection for ${ docName } with auth(${ webSocket . auth
315322 ? webSocket . auth . substring ( 0 , webSocket . auth . indexOf ( ' ' ) ) : 'none' } )`) ;
316323 const timingData = await setupWSConnection ( webSocket , docName , this . env , this . storage ) ;
317324 return timingData ;
0 commit comments