Skip to content

Commit aa58441

Browse files
authored
Fix uncaught runtime error. Closes #309 (#310)
1 parent d1e505f commit aa58441

File tree

6 files changed

+91
-37
lines changed

6 files changed

+91
-37
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
> **Note**: odd version numbers, for example, `0.13.0`, are not included in this changelog. They are used to test the new features and fixes before the final release.
99
10-
## [1.0.0] - Unreleased
10+
## [1.0.1] - Unreleased
1111

1212
### Changed:
1313

1414
- Snippets: All snippets that reference schemas updated to use `v1.0.0` schema
1515

16+
### Fixed:
17+
18+
- Runtime error: Fixed issue where the extension would throw an uncaught exception when running
19+
1620
## [0.27.0] - 2025-06-30
1721

1822
### Added:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "dev-proxy-toolkit",
33
"displayName": "Dev Proxy Toolkit",
44
"description": "Makes it easy to create and update Dev Proxy configuration files.",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"publisher": "garrytrinder",
77
"engines": {
88
"vscode": "^1.101.0"

src/documents.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,42 @@ import { updateFileDiagnostics, updateConfigFileDiagnostics } from './diagnostic
55
export const registerDocumentListeners = (context: vscode.ExtensionContext, collection: vscode.DiagnosticCollection) => {
66
context.subscriptions.push(
77
vscode.workspace.onDidOpenTextDocument(document => {
8-
if (isProxyFile(document)) {
9-
updateFileDiagnostics(context, document, collection);
10-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
11-
}
12-
if (!isConfigFile(document)) {
13-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
14-
return;
15-
} else {
16-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
17-
updateConfigFileDiagnostics(context, document, collection);
8+
try {
9+
if (isProxyFile(document)) {
10+
updateFileDiagnostics(context, document, collection);
11+
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
12+
}
13+
if (!isConfigFile(document)) {
14+
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
15+
return;
16+
} else {
17+
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
18+
updateConfigFileDiagnostics(context, document, collection);
19+
}
20+
} catch (error) {
21+
console.error('Error handling document open:', error);
1822
}
1923
})
2024
);
2125

2226
context.subscriptions.push(
2327
vscode.workspace.onDidChangeTextDocument(event => {
24-
if (!isConfigFile(event.document) && !isProxyFile(event.document)) {
25-
collection.delete(event.document.uri);
26-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
27-
return;
28-
}
29-
if (isConfigFile(event.document)) {
30-
updateConfigFileDiagnostics(context, event.document, collection);
31-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
32-
return;
33-
}
34-
if (isProxyFile(event.document)) {
35-
updateFileDiagnostics(context, event.document, collection);
36-
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
28+
try {
29+
if (!isConfigFile(event.document) && !isProxyFile(event.document)) {
30+
collection.delete(event.document.uri);
31+
return;
32+
}
33+
if (isConfigFile(event.document)) {
34+
updateConfigFileDiagnostics(context, event.document, collection);
35+
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true);
36+
return;
37+
}
38+
if (isProxyFile(event.document)) {
39+
updateFileDiagnostics(context, event.document, collection);
40+
vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false);
41+
}
42+
} catch (error) {
43+
console.error('Error handling document change:', error);
3744
}
3845
})
3946
);
@@ -58,4 +65,4 @@ export const registerDocumentListeners = (context: vscode.ExtensionContext, coll
5865
});
5966
})
6067
);
61-
};
68+
};

src/extension.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { VersionPreference } from './enums';
1010
import { registerMcpServer } from './mcp';
1111
import { registerTaskProvider } from './taskprovider';
1212

13+
// Global variable to track the interval
14+
let statusBarInterval: NodeJS.Timeout | undefined;
15+
1316
export const activate = async (context: vscode.ExtensionContext): Promise<vscode.ExtensionContext> => {
1417

1518
const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit');
@@ -19,6 +22,8 @@ export const activate = async (context: vscode.ExtensionContext): Promise<vscode
1922
await updateGlobalState(context, versionPreference);
2023

2124
const collection = vscode.languages.createDiagnosticCollection('dev-proxy-toolkit');
25+
// Add collection to subscriptions for automatic disposal
26+
context.subscriptions.push(collection);
2227

2328
registerDocumentListeners(context, collection);
2429
registerCodeActions(context);
@@ -32,9 +37,33 @@ export const activate = async (context: vscode.ExtensionContext): Promise<vscode
3237

3338
updateStatusBar(context, statusBar);
3439

35-
setInterval(() => statusBarLoop(context, statusBar, versionPreference), 5000);
40+
// Store the interval reference for proper cleanup
41+
statusBarInterval = setInterval(() => {
42+
// Add error handling to prevent uncaught exceptions
43+
try {
44+
statusBarLoop(context, statusBar, versionPreference);
45+
} catch (error) {
46+
console.error('Error in statusBarLoop:', error);
47+
}
48+
}, 5000);
49+
50+
// Add the interval to subscriptions for automatic cleanup
51+
context.subscriptions.push({
52+
dispose: () => {
53+
if (statusBarInterval) {
54+
clearInterval(statusBarInterval);
55+
statusBarInterval = undefined;
56+
}
57+
}
58+
});
3659

3760
return context;
3861
};
3962

40-
export const deactivate = () => { };
63+
export const deactivate = () => {
64+
// Clean up the interval if it's still running
65+
if (statusBarInterval) {
66+
clearInterval(statusBarInterval);
67+
statusBarInterval = undefined;
68+
}
69+
};

src/statusbar.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,24 @@ export const handleStatusBarUpdate = (context: vscode.ExtensionContext, statusBa
4646
};
4747

4848
export const statusBarLoop = async (context: vscode.ExtensionContext, statusBar: vscode.StatusBarItem, versionPreference: VersionPreference) => {
49-
const devProxyExe = getDevProxyExe(versionPreference);
50-
const isRunning = await isDevProxyRunning(devProxyExe);
51-
const globalState = context.globalState.get<DevProxyInstall>('devProxyInstall');
52-
await context.globalState.update('devProxyInstall', { ...globalState, isRunning });
53-
vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning);
54-
updateStatusBar(context, statusBar);
55-
};
49+
try {
50+
// Check if the context is still valid
51+
if (!context || !statusBar) {
52+
return;
53+
}
54+
55+
const devProxyExe = getDevProxyExe(versionPreference);
56+
const isRunning = await isDevProxyRunning(devProxyExe);
57+
const globalState = context.globalState.get<DevProxyInstall>('devProxyInstall');
58+
59+
// Only update if context is still valid
60+
if (context.globalState) {
61+
await context.globalState.update('devProxyInstall', { ...globalState, isRunning });
62+
vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning);
63+
updateStatusBar(context, statusBar);
64+
}
65+
} catch (error) {
66+
// Log but don't throw to prevent extension crashes
67+
console.error('Error in statusBarLoop:', error);
68+
}
69+
};

0 commit comments

Comments
 (0)