Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/fs): missing uv error context for readFile #27011

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/node/polyfills/_fs/_fs_readFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })));
}
}

Expand Down Expand Up @@ -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") {
Expand Down
25 changes: 24 additions & 1 deletion tests/unit_node/_fs/_fs_readFile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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/);
}
}
});