Skip to content

Commit

Permalink
Disconnect if newly connected server doesn't support console type (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Aug 21, 2024
1 parent 4f5b8cb commit 55936c3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export const ICON_ID = {
runAll: 'run-all',
runSelection: 'run',
server: 'server',
runningServer: 'circle-large-filled',
stoppedServer: 'circle-slash',
serverConnected: 'circle-large-filled',
serverRunning: 'circle-large-outline',
serverStopped: 'circle-slash',
} as const;

export const CONNECTION_TREE_ITEM_CONTEXT = {
Expand Down
6 changes: 6 additions & 0 deletions src/common/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class UnsupportedConsoleTypeError extends Error {
constructor(message: string) {
super(message);
this.name = 'UnsupportedConsoleTypeError';
}
}
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './commands';
export * from './constants';
export * from './errors';
36 changes: 26 additions & 10 deletions src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
updateConnectionStatusBarItem,
type ConnectionOption,
} from '../util';
import { UnsupportedConsoleTypeError } from '../common';

const logger = new Logger('ConnectionController');

Expand Down Expand Up @@ -138,6 +139,8 @@ export class ConnectionController implements Disposable {
): Promise<void> => {
updateConnectionStatusBarItem(this.connectStatusBarItem, 'connecting');

let newConnectionUrl: URL | null = null;

if ('url' in connectionOrServer) {
const cn = await this._serverManager.connectToServer(
connectionOrServer.url
Expand All @@ -148,9 +151,21 @@ export class ConnectionController implements Disposable {
}

connectionOrServer = cn;
newConnectionUrl = cn.serverUrl;
}

await this._serverManager.setEditorConnection(editor, connectionOrServer);
try {
await this._serverManager.setEditorConnection(editor, connectionOrServer);
} catch (err) {
updateConnectionStatusBarItem(this.connectStatusBarItem, 'disconnected');

// If our error was on a newly created connection, disconnect from it.
if (err instanceof UnsupportedConsoleTypeError && newConnectionUrl) {
this._serverManager.disconnectFromServer(newConnectionUrl);
}

throw err;
}

updateConnectionStatusBarItem(
this.connectStatusBarItem,
Expand Down Expand Up @@ -206,7 +221,7 @@ export class ConnectionController implements Disposable {
// If there are multiple options to select, prompt the user to select one.
const isSelected = await this.onPromptUserToSelectConnection();

// User cancelled the selection
// User cancelled the selection or an error occurred
if (!isSelected) {
return null;
}
Expand Down Expand Up @@ -259,16 +274,17 @@ export class ConnectionController implements Disposable {
editor.document.languageId,
editorActiveConnectionUrl
);
} catch (err) {
this._toaster.error(String(err));
}

if (result == null) {
return false;
}
if (result == null) {
return false;
}

await this.connectEditor(result.data, editor);
await this.connectEditor(result.data, editor);

return true;
return true;
} catch (err) {
this._toaster.error(err instanceof Error ? err.message : String(err));
return false;
}
};
}
8 changes: 6 additions & 2 deletions src/providers/ServerTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ export class ServerTreeProvider extends TreeDataProviderBase<ServerNode> {

return {
label: new URL(urlStr).host,
description: element.type === 'DHC' ? undefined : 'Enterprise',
description: isConnected ? '(1)' : undefined, // element.type === 'DHC' ? undefined : 'Enterprise',
tooltip: canConnect ? `Click to connect to ${urlStr}` : urlStr,
contextValue: element.type === 'DHC' ? contextValue : undefined,
iconPath: new vscode.ThemeIcon(
isRunning ? ICON_ID.runningServer : ICON_ID.stoppedServer
isRunning
? isConnected
? ICON_ID.serverConnected
: ICON_ID.serverRunning
: ICON_ID.serverStopped
),
command: canConnect
? {
Expand Down
9 changes: 6 additions & 3 deletions src/services/ServerManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as vscode from 'vscode';
import { SERVER_STATUS_CHECK_INTERVAL } from '../common';
import {
SERVER_STATUS_CHECK_INTERVAL,
UnsupportedConsoleTypeError,
} from '../common';
import { isDhcServerRunning } from '../dh/dhc';
import { isDheServerRunning } from '../dh/dhe';
import type {
Expand Down Expand Up @@ -221,8 +224,8 @@ export class ServerManager implements IServerManager {
editor.document.languageId as ConsoleType
))
) {
throw new Error(
`Connection '${dhService.serverUrl}' does not support the console type of the editor.`
throw new UnsupportedConsoleTypeError(
`Connection '${dhService.serverUrl}' does not support '${editor.document.languageId}'.`
);
}

Expand Down

0 comments on commit 55936c3

Please sign in to comment.