From 003e11b5f4588066405731d5cbd38afa01ebf904 Mon Sep 17 00:00:00 2001 From: Garry Trinder Date: Fri, 25 Jul 2025 17:36:29 +0100 Subject: [PATCH] Fix uncaught runtime error. Closes #309 Closes #309 --- CHANGELOG.md | 6 +++++- package-lock.json | 4 ++-- package.json | 2 +- src/documents.ts | 55 ++++++++++++++++++++++++++--------------------- src/extension.ts | 33 ++++++++++++++++++++++++++-- src/statusbar.ts | 28 ++++++++++++++++++------ 6 files changed, 91 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 338751b..6982e24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > **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. -## [1.0.0] - Unreleased +## [1.0.1] - Unreleased ### Changed: - Snippets: All snippets that reference schemas updated to use `v1.0.0` schema +### Fixed: + +- Runtime error: Fixed issue where the extension would throw an uncaught exception when running + ## [0.27.0] - 2025-06-30 ### Added: diff --git a/package-lock.json b/package-lock.json index 45fc853..7b9438d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dev-proxy-toolkit", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dev-proxy-toolkit", - "version": "1.0.0", + "version": "1.0.1", "dependencies": { "json-to-ast": "2.1.0", "semver": "7.7.2" diff --git a/package.json b/package.json index b2a0c71..761179d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "dev-proxy-toolkit", "displayName": "Dev Proxy Toolkit", "description": "Makes it easy to create and update Dev Proxy configuration files.", - "version": "1.0.0", + "version": "1.0.1", "publisher": "garrytrinder", "engines": { "vscode": "^1.101.0" diff --git a/src/documents.ts b/src/documents.ts index 0d4aa9b..bef226a 100644 --- a/src/documents.ts +++ b/src/documents.ts @@ -5,35 +5,42 @@ import { updateFileDiagnostics, updateConfigFileDiagnostics } from './diagnostic export const registerDocumentListeners = (context: vscode.ExtensionContext, collection: vscode.DiagnosticCollection) => { context.subscriptions.push( vscode.workspace.onDidOpenTextDocument(document => { - if (isProxyFile(document)) { - updateFileDiagnostics(context, document, collection); - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); - } - if (!isConfigFile(document)) { - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); - return; - } else { - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true); - updateConfigFileDiagnostics(context, document, collection); + try { + if (isProxyFile(document)) { + updateFileDiagnostics(context, document, collection); + vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); + } + if (!isConfigFile(document)) { + vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); + return; + } else { + vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true); + updateConfigFileDiagnostics(context, document, collection); + } + } catch (error) { + console.error('Error handling document open:', error); } }) ); context.subscriptions.push( vscode.workspace.onDidChangeTextDocument(event => { - if (!isConfigFile(event.document) && !isProxyFile(event.document)) { - collection.delete(event.document.uri); - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); - return; - } - if (isConfigFile(event.document)) { - updateConfigFileDiagnostics(context, event.document, collection); - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true); - return; - } - if (isProxyFile(event.document)) { - updateFileDiagnostics(context, event.document, collection); - vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); + try { + if (!isConfigFile(event.document) && !isProxyFile(event.document)) { + collection.delete(event.document.uri); + return; + } + if (isConfigFile(event.document)) { + updateConfigFileDiagnostics(context, event.document, collection); + vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', true); + return; + } + if (isProxyFile(event.document)) { + updateFileDiagnostics(context, event.document, collection); + vscode.commands.executeCommand('setContext', 'isDevProxyConfigFile', false); + } + } catch (error) { + console.error('Error handling document change:', error); } }) ); @@ -58,4 +65,4 @@ export const registerDocumentListeners = (context: vscode.ExtensionContext, coll }); }) ); -}; \ No newline at end of file +}; diff --git a/src/extension.ts b/src/extension.ts index 5085a83..57bf7c2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,6 +10,9 @@ import { VersionPreference } from './enums'; import { registerMcpServer } from './mcp'; import { registerTaskProvider } from './taskprovider'; +// Global variable to track the interval +let statusBarInterval: NodeJS.Timeout | undefined; + export const activate = async (context: vscode.ExtensionContext): Promise => { const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit'); @@ -19,6 +22,8 @@ export const activate = async (context: vscode.ExtensionContext): Promise statusBarLoop(context, statusBar, versionPreference), 5000); + // Store the interval reference for proper cleanup + statusBarInterval = setInterval(() => { + // Add error handling to prevent uncaught exceptions + try { + statusBarLoop(context, statusBar, versionPreference); + } catch (error) { + console.error('Error in statusBarLoop:', error); + } + }, 5000); + + // Add the interval to subscriptions for automatic cleanup + context.subscriptions.push({ + dispose: () => { + if (statusBarInterval) { + clearInterval(statusBarInterval); + statusBarInterval = undefined; + } + } + }); return context; }; -export const deactivate = () => { }; +export const deactivate = () => { + // Clean up the interval if it's still running + if (statusBarInterval) { + clearInterval(statusBarInterval); + statusBarInterval = undefined; + } +}; diff --git a/src/statusbar.ts b/src/statusbar.ts index 0cda3f9..42a56e6 100644 --- a/src/statusbar.ts +++ b/src/statusbar.ts @@ -46,10 +46,24 @@ export const handleStatusBarUpdate = (context: vscode.ExtensionContext, statusBa }; export const statusBarLoop = async (context: vscode.ExtensionContext, statusBar: vscode.StatusBarItem, versionPreference: VersionPreference) => { - const devProxyExe = getDevProxyExe(versionPreference); - const isRunning = await isDevProxyRunning(devProxyExe); - const globalState = context.globalState.get('devProxyInstall'); - await context.globalState.update('devProxyInstall', { ...globalState, isRunning }); - vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning); - updateStatusBar(context, statusBar); -}; \ No newline at end of file + try { + // Check if the context is still valid + if (!context || !statusBar) { + return; + } + + const devProxyExe = getDevProxyExe(versionPreference); + const isRunning = await isDevProxyRunning(devProxyExe); + const globalState = context.globalState.get('devProxyInstall'); + + // Only update if context is still valid + if (context.globalState) { + await context.globalState.update('devProxyInstall', { ...globalState, isRunning }); + vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning); + updateStatusBar(context, statusBar); + } + } catch (error) { + // Log but don't throw to prevent extension crashes + console.error('Error in statusBarLoop:', error); + } +};