Browse Source

Add new IConnectionLost token to handle a lost connection to the server.

Ian Rose 6 years ago
parent
commit
7e79346ec2

+ 24 - 2
packages/application-extension/src/index.tsx

@@ -2,6 +2,8 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
+  ConnectionLost,
+  IConnectionLost,
   ILabShell,
   ILabStatus,
   ILayoutRestorer,
@@ -73,10 +75,11 @@ namespace CommandIDs {
  */
 const main: JupyterFrontEndPlugin<void> = {
   id: '@jupyterlab/application-extension:main',
-  requires: [ICommandPalette, IRouter, IWindowResolver],
+  requires: [ICommandPalette, IConnectionLost, IRouter, IWindowResolver],
   activate: (
     app: JupyterFrontEnd,
     palette: ICommandPalette,
+    connectionLost: IConnectionLost,
     router: IRouter,
     resolver: IWindowResolver
   ) => {
@@ -108,6 +111,12 @@ const main: JupyterFrontEndPlugin<void> = {
       app.commands.notifyCommandChanged();
     });
 
+    // If the connection to the server is lost, handle it with the
+    // connection lost token.
+    app.serviceManager.terminals.connectionFailure.connect(
+      connectionLost as any
+    );
+
     const builder = app.serviceManager.builder;
     const build = () => {
       return builder
@@ -741,6 +750,18 @@ const paths: JupyterFrontEndPlugin<JupyterFrontEnd.IPaths> = {
   provides: JupyterFrontEnd.IPaths
 };
 
+/**
+ * The default JupyterLab paths dictionary provider.
+ */
+const connectionlost: JupyterFrontEndPlugin<IConnectionLost> = {
+  id: '@jupyterlab/apputils-extension:connectionlost',
+  activate: (app: JupyterFrontEnd): IConnectionLost => {
+    return ConnectionLost;
+  },
+  autoStart: true,
+  provides: IConnectionLost
+};
+
 /**
  * Export the plugins as default.
  */
@@ -755,7 +776,8 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
   shell,
   status,
   info,
-  paths
+  paths,
+  connectionlost
 ];
 
 export default plugins;

+ 38 - 0
packages/application/src/connectionlost.ts

@@ -0,0 +1,38 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { showErrorMessage } from '@jupyterlab/apputils';
+
+import { ServerConnection, SessionManager } from '@jupyterlab/services';
+
+import { IConnectionLost } from './tokens';
+
+export const ConnectionLost: IConnectionLost = async function(
+  manager: SessionManager,
+  err: ServerConnection.NetworkError
+): Promise<void> {
+  if (Private.showingError) {
+    return;
+  }
+  Private.showingError = true;
+
+  const title = 'Server Connection Error';
+  const networkMsg =
+    'A connection to the Jupyter server could not be established.\n' +
+    'JupyterLab will continue trying to reconnect.\n' +
+    'Check your network connection or Jupyter server configuration.\n';
+
+  return showErrorMessage(title, { message: networkMsg }).then(() => {
+    Private.showingError = false;
+  });
+};
+
+/**
+ * A namespace for module private functionality.
+ */
+namespace Private {
+  /**
+   * Whether the connection lost error is currently being shown.
+   */
+  export let showingError = false;
+}

+ 4 - 0
packages/application/src/index.ts

@@ -4,6 +4,8 @@
 // Local CSS must be loaded prior to loading other libs.
 import '../style/index.css';
 
+export { ConnectionLost } from './connectionlost';
+
 export { JupyterFrontEnd, JupyterFrontEndPlugin } from './frontend';
 
 export { JupyterLab } from './lab';
@@ -17,3 +19,5 @@ export { IRouter, Router } from './router';
 export { ILabShell, LabShell } from './shell';
 
 export { ILabStatus } from './status';
+
+export { IConnectionLost } from './tokens';

+ 19 - 0
packages/application/src/tokens.ts

@@ -0,0 +1,19 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { ServerConnection, SessionManager } from '@jupyterlab/services';
+
+import { Token } from '@phosphor/coreutils';
+
+/**
+ * A token for which a plugin can provide to respond to connection failures
+ * to the application server.
+ */
+export const IConnectionLost = new Token<IConnectionLost>(
+  '@jupyterlab/apputils:IConnectionLost'
+);
+
+export type IConnectionLost = (
+  manager: SessionManager,
+  err: ServerConnection.NetworkError
+) => Promise<void>;

+ 11 - 23
packages/filebrowser/src/browser.ts

@@ -244,30 +244,18 @@ export class FileBrowser extends Widget {
    * Handle a connection lost signal from the model.
    */
   private _onConnectionFailure(sender: FileBrowserModel, args: Error): void {
-    if (this._showingError) {
-      return;
-    }
-    this._showingError = true;
-
-    let title = 'Server Connection Error';
-    let networkMsg =
-      'A connection to the Jupyter server could not be established.\n' +
-      'JupyterLab will continue trying to reconnect.\n' +
-      'Check your network connection or Jupyter server configuration.\n';
-
-    // Check for a fetch error.
-    if (args instanceof ServerConnection.NetworkError) {
-      args.message = networkMsg;
-    } else if (args instanceof ServerConnection.ResponseError) {
-      if (args.response.status === 404) {
-        title = 'Directory not found';
-        args.message = `Directory not found: "${this.model.path}"`;
-      }
+    if (
+      !this._showingError &&
+      args instanceof ServerConnection.ResponseError &&
+      args.response.status === 404
+    ) {
+      const title = 'Directory not found';
+      args.message = `Directory not found: "${this.model.path}"`;
+      this._showingError = true;
+      void showErrorMessage(title, args).then(() => {
+        this._showingError = false;
+      });
     }
-
-    void showErrorMessage(title, args).then(() => {
-      this._showingError = false;
-    });
   }
 
   private _crumbs: BreadCrumbs;

+ 0 - 1
packages/services/src/kernel/manager.ts

@@ -282,7 +282,6 @@ export class KernelManager implements Kernel.IManager {
     const models = await Kernel.listRunning(this.serverSettings).catch(err => {
       if (err instanceof ServerConnection.NetworkError) {
         this._connectionFailure.emit(err);
-        console.log('Connection Failure');
         return [] as Kernel.IModel[];
       }
       throw err;

+ 0 - 1
packages/services/src/terminal/manager.ts

@@ -248,7 +248,6 @@ export class TerminalManager implements TerminalSession.IManager {
       err => {
         if (err instanceof ServerConnection.NetworkError) {
           this._connectionFailure.emit(err);
-          console.log('Connection Failure');
           return [] as TerminalSession.IModel[];
         }
         throw err;