mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
fix(ext/node): convert errors from fs.readFile/fs.readFileSync
to node 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](ce30b2be34/node-src/lib/getConfiguration.ts (L88-L92)
)
thrown here to check for a file not found error.
This commit is contained in:
parent
90edca21a2
commit
951103fc8d
1 changed files with 8 additions and 2 deletions
|
@ -19,6 +19,7 @@ import {
|
||||||
TextEncodings,
|
TextEncodings,
|
||||||
} from "ext:deno_node/_utils.ts";
|
} from "ext:deno_node/_utils.ts";
|
||||||
import { FsFile } from "ext:deno_fs/30_fs.js";
|
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(data: Uint8Array, encoding: TextEncodings): string;
|
||||||
function maybeDecode(
|
function maybeDecode(
|
||||||
|
@ -87,7 +88,7 @@ export function readFile(
|
||||||
}
|
}
|
||||||
const buffer = maybeDecode(data, encoding);
|
const buffer = maybeDecode(data, encoding);
|
||||||
(cb as BinaryCallback)(null, buffer);
|
(cb as BinaryCallback)(null, buffer);
|
||||||
}, (err) => cb && cb(err));
|
}, (err) => cb && cb(denoErrorToNodeError(err)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +118,12 @@ export function readFileSync(
|
||||||
opt?: FileOptionsArgument,
|
opt?: FileOptionsArgument,
|
||||||
): string | Buffer {
|
): string | Buffer {
|
||||||
path = path instanceof URL ? pathFromURL(path) : path;
|
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);
|
const encoding = getEncoding(opt);
|
||||||
if (encoding && encoding !== "binary") {
|
if (encoding && encoding !== "binary") {
|
||||||
const text = maybeDecode(data, encoding);
|
const text = maybeDecode(data, encoding);
|
||||||
|
|
Loading…
Reference in a new issue