Skip to content

Commit 571eda9

Browse files
kptdobebosschaert
andauthored
chore: improve logging (#82)
Co-authored-by: David Bosschaert <bosschae@adobe.com>
1 parent d78a36c commit 571eda9

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

src/edge.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/shareddoc.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export const readState = async (docName, storage) => {
8787
}
8888

8989
if (stored.has('docstore')) {
90+
// eslint-disable-next-line no-console
91+
console.log('Document found in persistence');
9092
return stored.get('docstore');
9193
}
9294

@@ -100,6 +102,8 @@ export const readState = async (docName, storage) => {
100102
data.push(chunk[j]);
101103
}
102104
}
105+
// eslint-disable-next-line no-console
106+
console.log('Document data read');
103107
return new Uint8Array(data);
104108
};
105109

@@ -182,7 +186,7 @@ export const persistence = {
182186
return null;
183187
} else {
184188
// eslint-disable-next-line no-console
185-
console.log(`unable to get resource: ${initialReq.status} - ${initialReq.statusText}`);
189+
console.error(`Unable to get resource from da-admin: ${initialReq.status} - ${initialReq.statusText}`);
186190
throw new Error(`unable to get resource - status: ${initialReq.status}`);
187191
}
188192
},
@@ -247,14 +251,12 @@ export const persistence = {
247251
closeAll = (status === 401 || status === 403);
248252
throw new Error(`${status} - ${statusText}`);
249253
}
250-
// eslint-disable-next-line no-console
251-
console.log(content);
252254

253255
return content;
254256
}
255257
} catch (err) {
256258
// eslint-disable-next-line no-console
257-
console.error(err);
259+
console.error('Failed to update document', err);
258260
showError(ydoc, err);
259261
}
260262
if (closeAll) {
@@ -320,7 +322,7 @@ export const persistence = {
320322
}
321323
} catch (error) {
322324
// eslint-disable-next-line no-console
323-
console.log('Problem restoring state from worker storage', error);
325+
console.error('Problem restoring state from worker storage', error);
324326
showError(ydoc, error);
325327
}
326328

@@ -342,7 +344,7 @@ export const persistence = {
342344
console.log('Restored from da-admin', docName);
343345
} catch (error) {
344346
// eslint-disable-next-line no-console
345-
console.log('Problem restoring state from da-admin', error);
347+
console.error('Problem restoring state from da-admin', error);
346348
showError(ydoc, error);
347349
}
348350
});
@@ -507,7 +509,7 @@ export const messageListener = (conn, doc, message) => {
507509
}
508510
} catch (err) {
509511
// eslint-disable-next-line no-console
510-
console.error(err);
512+
console.error('Error in messageListener', err);
511513
showError(doc, err);
512514
}
513515
};

0 commit comments

Comments
 (0)