Skip to content

Commit 436c5eb

Browse files
committed
improve error type safety
1 parent 57ff467 commit 436c5eb

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/cursorless-engine/src/nodeCommon/TalonSpokenFormsJsonReader.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class TalonSpokenFormsJsonReader implements TalonSpokenForms {
5050
await readFile(this.cursorlessTalonStateJsonPath, "utf-8"),
5151
);
5252
} catch (err) {
53-
if ((err as any)?.code === "ENOENT") {
53+
if (isErrnoException(err) && err.code === "ENOENT") {
5454
throw new NeedsInitialTalonUpdateError(
5555
`Custom spoken forms file not found at ${this.cursorlessTalonStateJsonPath}. Using default spoken forms.`,
5656
);
@@ -73,3 +73,15 @@ export class TalonSpokenFormsJsonReader implements TalonSpokenForms {
7373
this.disposer.dispose();
7474
}
7575
}
76+
77+
/**
78+
* A user-defined type guard function that checks if a given error is a
79+
* `NodeJS.ErrnoException`.
80+
*
81+
* @param {any} error - The error to check.
82+
* @returns {error is NodeJS.ErrnoException} - Returns `true` if the error is a
83+
* {@link NodeJS.ErrnoException}, otherwise `false`.
84+
*/
85+
function isErrnoException(error: any): error is NodeJS.ErrnoException {
86+
return error instanceof Error && "code" in error;
87+
}

0 commit comments

Comments
 (0)