Skip to content

Commit

Permalink
Catch more webview disposed errors
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
  • Loading branch information
paulober committed Sep 17, 2024
1 parent 9c6868c commit 864721d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
39 changes: 30 additions & 9 deletions src/webview/newMicroPythonProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import which from "which";
import { existsSync } from "fs";
import { join } from "path";
import { PythonExtension } from "@vscode/python-extension";
import { unknownErrorToString } from "../utils/errorHelper.mjs";

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

if (html !== "") {
this._panel.webview.html = html;
try {
this._panel.webview.html = html;
} catch (error) {
this._logger.error(
"Failed to set webview html. Webview might have been disposed. Error: ",
unknownErrorToString(error)
);
// properly dispose panel
this.dispose();

return;
}
await this._updateTheme();
} else {
void window.showErrorMessage(
Expand All @@ -390,14 +402,23 @@ print("Finished.")\r\n`;
}

private async _updateTheme(): Promise<void> {
await this._panel.webview.postMessage({
command: "setTheme",
theme:
window.activeColorTheme.kind === ColorThemeKind.Dark ||
window.activeColorTheme.kind === ColorThemeKind.HighContrast
? "dark"
: "light",
});
try {
await this._panel.webview.postMessage({
command: "setTheme",
theme:
window.activeColorTheme.kind === ColorThemeKind.Dark ||
window.activeColorTheme.kind === ColorThemeKind.HighContrast
? "dark"
: "light",
});
} catch (error) {
this._logger.error(
"Failed to update theme in webview. Webview might have been disposed. Error:",
unknownErrorToString(error)
);
// properly dispose panel
this.dispose();
}
}

public dispose(): void {
Expand Down
25 changes: 17 additions & 8 deletions src/webview/newProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,14 +1233,23 @@ export class NewProjectPanel {
}

private async _updateTheme(): Promise<void> {
await this._panel.webview.postMessage({
command: "setTheme",
theme:
window.activeColorTheme.kind === ColorThemeKind.Dark ||
window.activeColorTheme.kind === ColorThemeKind.HighContrast
? "dark"
: "light",
});
try {
await this._panel.webview.postMessage({
command: "setTheme",
theme:
window.activeColorTheme.kind === ColorThemeKind.Dark ||
window.activeColorTheme.kind === ColorThemeKind.HighContrast
? "dark"
: "light",
});
} catch (error) {
this._logger.error(
"Failed to update theme in webview. Webview might have been disposed. Error:",
unknownErrorToString(error)
);
// properly dispose panel
this.dispose();
}
}

public dispose(): void {
Expand Down

0 comments on commit 864721d

Please sign in to comment.