Skip to content

Commit a2671b2

Browse files
hbcarlosfcollonval
andauthored
Improves error handling when the file does not exist (#175)
* Improves error handling when the file does not exist * Review * Update jupyter_collaboration/rooms.py Co-authored-by: Frédéric Collonval <[email protected]> --------- Co-authored-by: Frédéric Collonval <[email protected]>
1 parent b43b525 commit a2671b2

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

jupyter_collaboration/handlers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,22 @@ async def open(self, room_id):
185185
except Exception as e:
186186
_, _, file_id = decode_file_path(self._room_id)
187187
file = self._file_loaders[file_id]
188-
self.log.error(f"Error initializing: {file.path}\n{e!r}", exc_info=e)
189-
self.close(
190-
1003, f"Error initializing: {file.path}. You need to close the document."
191-
)
188+
189+
# Close websocket and propagate error.
190+
if isinstance(e, web.HTTPError):
191+
self.log.error(f"File {file.path} not found.\n{e!r}", exc_info=e)
192+
self.close(1004, f"File {file.path} not found.")
193+
else:
194+
self.log.error(f"Error initializing: {file.path}\n{e!r}", exc_info=e)
195+
self.close(
196+
1003, f"Error initializing: {file.path}. You need to close the document."
197+
)
198+
199+
# Clean up the room and delete the file loader
200+
if len(self.room.clients) == 0 or self.room.clients == [self]:
201+
self._message_queue.put_nowait(b"")
202+
self._cleanup_delay = 0
203+
await self._clean_room()
192204

193205
self._emit(LogLevel.INFO, "initialize", "New client connected.")
194206

jupyter_collaboration/rooms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ def _emit(self, level: LogLevel, action: str | None = None, msg: str | None = No
203203

204204
self._logger.emit(schema_id=JUPYTER_COLLABORATION_EVENTS_URI, data=data)
205205

206-
def _clean(self) -> None:
206+
def stop(self) -> None:
207207
"""
208-
Cleans the rooms.
208+
Stop the room.
209209
210210
Cancels the save task and unsubscribes from the file.
211211
"""
212-
super()._clean()
212+
super().stop()
213213
# TODO: Should we cancel or wait ?
214214
if self._saving_document:
215215
self._saving_document.cancel()

packages/docprovider/src/ydrive.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ export class YDrive extends Drive implements ICollaborativeDrive {
7878
const provider = this._providers.get(key);
7979

8080
if (provider) {
81-
const model = super.get(localPath, { ...options, content: false });
82-
await provider.ready;
81+
// If the document does't exist, `super.get` will reject with an
82+
// error and the provider will never be resolved.
83+
// Use `Promise.all` to reject as soon as possible. The Context will
84+
// show a dialog to the user.
85+
const [model] = await Promise.all([
86+
super.get(localPath, { ...options, content: false }),
87+
provider.ready
88+
]);
8389
return model;
8490
}
8591
}

0 commit comments

Comments
 (0)