Skip to content

Commit

Permalink
fix(ext/node): convert errors from fs.readFile/fs.readFileSync to n…
Browse files Browse the repository at this point in the history
…ode format (#26632)

Fixes the original issue reported in #26404. storybook runs into other
errors after this PR (the new errors will be fixed in other PRs).

Some code used by a dependency of storybook does a [string comparison on
the error
message](https://github.com/chromaui/chromatic-cli/blob/ce30b2be343cb96a0826390b95ea42befb2be547/node-src/lib/getConfiguration.ts#L88-L92)
thrown here to check for a file not found error.
  • Loading branch information
nathanwhit authored and bartlomieju committed Nov 5, 2024
1 parent 919a96d commit d284d1c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ext/node/polyfills/_fs/_fs_readFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
TextEncodings,
} from "ext:deno_node/_utils.ts";
import { FsFile } from "ext:deno_fs/30_fs.js";
import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts";

function maybeDecode(data: Uint8Array, encoding: TextEncodings): string;
function maybeDecode(
Expand Down Expand Up @@ -87,7 +88,7 @@ export function readFile(
}
const buffer = maybeDecode(data, encoding);
(cb as BinaryCallback)(null, buffer);
}, (err) => cb && cb(err));
}, (err) => cb && cb(denoErrorToNodeError(err)));
}
}

Expand Down Expand Up @@ -117,7 +118,12 @@ export function readFileSync(
opt?: FileOptionsArgument,
): string | Buffer {
path = path instanceof URL ? pathFromURL(path) : path;
const data = Deno.readFileSync(path);
let data;
try {
data = Deno.readFileSync(path);
} catch (err) {
throw denoErrorToNodeError(err);
}
const encoding = getEncoding(opt);
if (encoding && encoding !== "binary") {
const text = maybeDecode(data, encoding);
Expand Down

0 comments on commit d284d1c

Please sign in to comment.