Skip to content

Commit 3cf11ab

Browse files
committed
refactor(R): use custom error class for x64-on-ARM detection
Replace string matching with type-safe instanceof checks: - Add WindowsArmX64RError custom error class - Update checkWindowsArmR() to throw custom error - Replace e.message.includes() checks with instanceof More robust and follows TypeScript best practices.
1 parent 94a74dc commit 3cf11ab

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/core/knitr.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,20 @@ export async function checkRBinary() {
7070
}
7171
}
7272

73+
export class WindowsArmX64RError extends Error {
74+
constructor(msg: string) {
75+
super(msg);
76+
}
77+
}
78+
7379
function checkWindowsArmR(platform: string | undefined): void {
7480
if (!platform) return;
7581

7682
const isWindowsArm = isWindows && Deno.build.arch === "aarch64";
7783
const isX64R = platform.includes("x86_64") || platform.includes("i386");
7884

7985
if (isWindowsArm && isX64R) {
80-
throw new Error(
86+
throw new WindowsArmX64RError(
8187
"x64 R detected on Windows ARM.\n\n" +
8288
"x64 R runs under emulation and is not reliable for Quarto.\n" +
8389
"Please install native ARM64 R. \n" +
@@ -150,7 +156,7 @@ export async function knitrCapabilities(rBin: string | undefined) {
150156
}
151157
} catch (e) {
152158
// If it's our specific x64-on-ARM error, rethrow it
153-
if (e instanceof Error && e.message.includes("x64 R detected")) {
159+
if (e instanceof WindowsArmX64RError) {
154160
throw e;
155161
}
156162
// Otherwise YAML parse failed, continue to return undefined
@@ -162,7 +168,7 @@ export async function knitrCapabilities(rBin: string | undefined) {
162168
}
163169
} catch (e) {
164170
// Rethrow x64-on-ARM errors - these have helpful messages
165-
if (e instanceof Error && e.message.includes("x64 R detected")) {
171+
if (e instanceof WindowsArmX64RError) {
166172
throw e;
167173
}
168174
debug(

0 commit comments

Comments
 (0)