Skip to content

Commit 864721d

Browse files
committed
Catch more webview disposed errors
Signed-off-by: paulober <[email protected]>
1 parent 9c6868c commit 864721d

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/webview/newMicroPythonProjectPanel.mts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import which from "which";
2323
import { existsSync } from "fs";
2424
import { join } from "path";
2525
import { PythonExtension } from "@vscode/python-extension";
26+
import { unknownErrorToString } from "../utils/errorHelper.mjs";
2627

2728
interface SubmitMessageValue {
2829
projectName: string;
@@ -379,7 +380,18 @@ print("Finished.")\r\n`;
379380
const html = await this._getHtmlForWebview(this._panel.webview);
380381

381382
if (html !== "") {
382-
this._panel.webview.html = html;
383+
try {
384+
this._panel.webview.html = html;
385+
} catch (error) {
386+
this._logger.error(
387+
"Failed to set webview html. Webview might have been disposed. Error: ",
388+
unknownErrorToString(error)
389+
);
390+
// properly dispose panel
391+
this.dispose();
392+
393+
return;
394+
}
383395
await this._updateTheme();
384396
} else {
385397
void window.showErrorMessage(
@@ -390,14 +402,23 @@ print("Finished.")\r\n`;
390402
}
391403

392404
private async _updateTheme(): Promise<void> {
393-
await this._panel.webview.postMessage({
394-
command: "setTheme",
395-
theme:
396-
window.activeColorTheme.kind === ColorThemeKind.Dark ||
397-
window.activeColorTheme.kind === ColorThemeKind.HighContrast
398-
? "dark"
399-
: "light",
400-
});
405+
try {
406+
await this._panel.webview.postMessage({
407+
command: "setTheme",
408+
theme:
409+
window.activeColorTheme.kind === ColorThemeKind.Dark ||
410+
window.activeColorTheme.kind === ColorThemeKind.HighContrast
411+
? "dark"
412+
: "light",
413+
});
414+
} catch (error) {
415+
this._logger.error(
416+
"Failed to update theme in webview. Webview might have been disposed. Error:",
417+
unknownErrorToString(error)
418+
);
419+
// properly dispose panel
420+
this.dispose();
421+
}
401422
}
402423

403424
public dispose(): void {

src/webview/newProjectPanel.mts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,14 +1233,23 @@ export class NewProjectPanel {
12331233
}
12341234

12351235
private async _updateTheme(): Promise<void> {
1236-
await this._panel.webview.postMessage({
1237-
command: "setTheme",
1238-
theme:
1239-
window.activeColorTheme.kind === ColorThemeKind.Dark ||
1240-
window.activeColorTheme.kind === ColorThemeKind.HighContrast
1241-
? "dark"
1242-
: "light",
1243-
});
1236+
try {
1237+
await this._panel.webview.postMessage({
1238+
command: "setTheme",
1239+
theme:
1240+
window.activeColorTheme.kind === ColorThemeKind.Dark ||
1241+
window.activeColorTheme.kind === ColorThemeKind.HighContrast
1242+
? "dark"
1243+
: "light",
1244+
});
1245+
} catch (error) {
1246+
this._logger.error(
1247+
"Failed to update theme in webview. Webview might have been disposed. Error:",
1248+
unknownErrorToString(error)
1249+
);
1250+
// properly dispose panel
1251+
this.dispose();
1252+
}
12441253
}
12451254

12461255
public dispose(): void {

0 commit comments

Comments
 (0)