diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index cf7e0305d82100..029e57c5029e0c 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -88,7 +88,7 @@ export function readFile( } const buffer = maybeDecode(data, encoding); (cb as BinaryCallback)(null, buffer); - }, (err) => cb && cb(denoErrorToNodeError(err))); + }, (err) => cb && cb(denoErrorToNodeError(err, { path, syscall: "open" }))); } } @@ -122,7 +122,7 @@ export function readFileSync( try { data = Deno.readFileSync(path); } catch (err) { - throw denoErrorToNodeError(err); + throw denoErrorToNodeError(err, { path, syscall: "open" }); } const encoding = getEncoding(opt); if (encoding && encoding !== "binary") { diff --git a/tests/unit_node/_fs/_fs_readFile_test.ts b/tests/unit_node/_fs/_fs_readFile_test.ts index ea36b9d866af4d..a75f12d1f6fe39 100644 --- a/tests/unit_node/_fs/_fs_readFile_test.ts +++ b/tests/unit_node/_fs/_fs_readFile_test.ts @@ -2,7 +2,7 @@ import { assertCallbackErrorUncaught } from "../_test_utils.ts"; import { promises, readFile, readFileSync } from "node:fs"; import * as path from "@std/path"; -import { assert, assertEquals } from "@std/assert"; +import { assert, assertEquals, assertMatch } from "@std/assert"; const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); @@ -121,3 +121,26 @@ Deno.test("fs.promises.readFile with no arg call rejects with error correctly", // @ts-ignore no arg call needs to be supported await promises.readFile().catch((_e) => {}); }); + +Deno.test("fs.readFile error message contains path + syscall", async () => { + const path = "/does/not/exist"; + const err = await new Promise((resolve) => { + readFile(path, "utf-8", (err) => resolve(err)); + }); + if (err instanceof Error) { + assert(err.message.includes(path), "Path not found in error message"); + assertMatch(err.message, /[,\s]open\s/); + } +}); + +Deno.test("fs.readFileSync error message contains path + syscall", () => { + const path = "/does/not/exist"; + try { + readFileSync(path, "utf-8"); + } catch (err) { + if (err instanceof Error) { + assert(err.message.includes(path), "Path not found in error message"); + assertMatch(err.message, /[,\s]open\s/); + } + } +});