Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling in extensions to visual studio code

Can someone point me to best practices for error handling in a Visual Studio Code extension?

I'm writing an extension in TypeScript that contributes a debugger. I want to log unexpected behavior, sometimes as information to the user explaining that something didn't go right, sometimes to create a trail for debugging, but certainly not to fail silently. Using console.log or console.error shows up in the debug output when I am debugging the extension, but I can't find it when the extension is installed. Do I have to open an output channel specifically for my extension and write everything there? Should I be throwing up showInformationMessage and showErrorMessage windows? Should I just be throwing exceptions and hope that code will do the right thing?

like image 901
Mark R. Tuttle Avatar asked Oct 16 '25 23:10

Mark R. Tuttle


1 Answers

In my extension I use two ways for feedback. One is an output channel for errors produced by an external process that I'm using for the work.

Create the channel in your activation method:

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

and push output to it whenever you have something:

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

                    return;
                }

The other one is what you already considered:

        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;
        }

which is for messages I create in the extension and that users need to see and take seriously.

like image 157
Mike Lischke Avatar answered Oct 19 '25 14:10

Mike Lischke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!