Visual Studio 代码扩展中的错误处理

时间:2021-04-09 01:44:08

标签: visual-studio-code vscode-extensions

有人可以向我指出 Visual Studio Code 扩展中错误处理的最佳实践吗?

我正在用 TypeScript 编写一个提供调试器的扩展。我想记录意外行为,有时作为向用户解释某些事情不正确的信息,有时是为了创建调试路径,但肯定不会无声地失败。当我调试扩展时,使用 console.logconsole.error 显示在调试输出中,但在安装扩展时我找不到它。我是否必须专门为我的扩展打开一个输出通道并在那里写入所有内容?我应该吐出 showInformationMessageshowErrorMessage 窗口吗?我应该只是抛出异常并希望代码做正确的事情吗?

1 个答案:

答案 0 :(得分:0)

在我的扩展中,我使用两种反馈方式。一个是我用于工作的外部流程产生的错误的输出渠道。

在您的激活方法中创建频道:

outputChannel = window.createOutputChannel("ANTLR4 Errors");

并在您有东西时将输出推送给它:

                } catch (reason) {
                    outputChannel.appendLine("Cannot parse sentence generation config file:");
                    outputChannel.appendLine((reason as SyntaxError).message);
                    outputChannel.show(true);

                    return;
                }

另一个是你已经考虑过的:

        if (workspace.getConfiguration("antlr4.generation").mode === "none") {
            void window.showErrorMessage("Interpreter data generation is disabled in the preferences (see " +
                "'antlr4.generation'). Set this at least to 'internal' to enable debugging.");

            return null;
        }

这是我在扩展程序中创建的消息,用户需要查看并认真对待。