iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦕
Creating Exception Handling Rules with a Deno Lint Plugin
I created a deno-lint-plugin that enforces functions starting with do to be wrapped in a try-catch block.
Motivations
- Gemini/Claude are bad at exception design.
- They wrap everything in
try-catchand swallow exceptions. - I want to express exceptions explicitly.
- They wrap everything in
-
neverthrowis overkill. - TS type signatures are difficult because exceptions don't appear in them.
Inspiration: vscode's doGet~
This is a pattern I saw when reading the VS Code source code. Functions starting with do were explicitly shown to throw exceptions, which I found quite intuitive.
async function doGetLogs(fileService: IFileService, logs: ILogFile[], curFolder: URI, logsHome: URI): Promise<void> {
...
}
More specifically, it's like Java's checked exceptions.
public void throwableMethod(String name) throws Exception {...}
Implementation with deno-lint-plugin
{
"lint": {
"plugins": ["jsr:@mizchi/deno-lint-plugin-do-try"]
}
}
I implemented two rules:
-
do-try/require-try-catch-for-do-functions- Calls to functions starting with
domust always be wrapped in atry catchblock.
- Calls to functions starting with
-
do-try/throw-needs-do-prefix- Function names containing a
throwstatement must start withdo.
- Function names containing a
The reason it's not an ESLint plugin is just because I wanted to try making a deno-lint-plugin...
async function doSomething() {}
const obj = { doSomething };
// NG
doSomething();
// NG
await doSomething();
// NG
await obj.doSomething();
try {
const run = async () => {
// NG
await doSomething();
};
await run();
} catch (error) {
console.error("An error occurred:", error);
}
try {
// OK
doSomething();
} catch (error) {
console.error("An error occurred:", error);
}
// NG
// deno-lint-ignore no-unused-vars
function throwableFunction() {
if (Math.random() > 0.5) {
throw new Error("Random error");
}
return "data";
}
// OK
// deno-lint-ignore no-unused-vars
function doThrowableFunction() {
if (Math.random() > 0.5) {
throw new Error("Random error");
}
return "data";
}
Execution results
$ deno lint examples/main.ts
error[do-try/require-try-catch-for-do-functions]: Function 'doSomething' starts with 'do', so it must be wrapped in a try-catch block
--> /home/mizchi/sandbox/deno-plugin/examples/main.ts:4:1
|
4 | doSomething();
| ^^^^^^^^^^^^^
error[do-try/require-try-catch-for-do-functions]: Function 'doSomething' starts with 'do', so it must be wrapped in a try-catch block
--> /home/mizchi/sandbox/deno-plugin/examples/main.ts:5:1
|
5 | await doSomething();
| ^^^^^^^^^^^^^^^^^^^
error[do-try/require-try-catch-for-do-functions]: Function 'doSomething' starts with 'do', so it must be wrapped in a try-catch block
--> /home/mizchi/sandbox/deno-plugin/examples/main.ts:6:1
|
6 | await obj.doSomething();
| ^^^^^^^^^^^^^^^^^^^^^^^
error[do-try/require-try-catch-for-do-functions]: Function 'doSomething' starts with 'do', so it must be wrapped in a try-catch block
--> /home/mizchi/sandbox/deno-plugin/examples/main.ts:10:5
|
10 | await doSomething();
| ^^^^^^^^^^^^^^^^^^^
error[do-try/throw-needs-do-prefix]: Function 'throwableFunction' contains throw statement, so it must start with 'do'
--> /home/mizchi/sandbox/deno-plugin/examples/main.ts:24:1
|
24 | function throwableFunction() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
25 | if (Math.random() > 0.5) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
26 | throw new Error("Random error");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
27 | }
| ^^^
|
28 | return "data";
| ^^^^^^^^^^^^^^^^
|
29 | }
| ^
I haven't used it seriously in production yet, but since it expresses my intentions well, I'll keep using it while making minor adjustments.
Bonus: How it was made
First, I had the AI read this:
Then, I provided some few-shot examples of valid and invalid code, and by the time I finished dinner, it was done.
Discussion