Skip to content

Commit 2a60184

Browse files
authored
feat(fs/walk): WalkError class (#3054)
1 parent 31474e7 commit 2a60184

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

fs/walk.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ import {
1111
WalkEntry,
1212
} from "./_util.ts";
1313

14+
export class WalkError extends Error {
15+
override cause: unknown;
16+
override name = "WalkError";
17+
path: string;
18+
19+
constructor(cause: unknown, path: string) {
20+
super(
21+
`${cause instanceof Error ? cause.message : cause} for path "${path}"`,
22+
);
23+
this.path = path;
24+
this.cause = cause;
25+
}
26+
}
27+
1428
function include(
1529
path: string,
1630
exts?: string[],
@@ -29,16 +43,9 @@ function include(
2943
return true;
3044
}
3145

32-
function wrapErrorWithRootPath(err: unknown, root: string) {
33-
if (err instanceof Error && "root" in err) return err;
34-
const e = new Error() as Error & { root: string };
35-
e.root = root;
36-
e.message = err instanceof Error
37-
? `${err.message} for path "${root}"`
38-
: `[non-error thrown] for path "${root}"`;
39-
e.stack = err instanceof Error ? err.stack : undefined;
40-
e.cause = err instanceof Error ? err.cause : undefined;
41-
return e;
46+
function wrapErrorWithPath(err: unknown, root: string) {
47+
if (err instanceof WalkError) return err;
48+
return new WalkError(err, root);
4249
}
4350

4451
export interface WalkOptions {
@@ -124,7 +131,7 @@ export async function* walk(
124131
}
125132
}
126133
} catch (err) {
127-
throw wrapErrorWithRootPath(err, normalize(root));
134+
throw wrapErrorWithPath(err, normalize(root));
128135
}
129136
}
130137

@@ -155,7 +162,7 @@ export function* walkSync(
155162
try {
156163
entries = Deno.readDirSync(root);
157164
} catch (err) {
158-
throw wrapErrorWithRootPath(err, normalize(root));
165+
throw wrapErrorWithPath(err, normalize(root));
159166
}
160167
for (const entry of entries) {
161168
assert(entry.name != null);

fs/walk_test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22
// deno-lint-ignore-file no-explicit-any
3-
import { walk, WalkEntry, WalkOptions, walkSync } from "./walk.ts";
3+
import { walk, WalkEntry, WalkError, WalkOptions, walkSync } from "./walk.ts";
44
import {
55
assert,
66
assertEquals,
@@ -267,6 +267,21 @@ testWalk(
267267
},
268268
);
269269

270+
testWalk(
271+
async (d: string) => {
272+
await Deno.mkdir(d + "/a");
273+
await touch(d + "/a/x");
274+
await touch(d + "/a/y");
275+
},
276+
async function walkError() {
277+
await assertRejects(async () => {
278+
for await (const _walkEntry of walk("./a")) {
279+
await Deno.remove("./a", { recursive: true });
280+
}
281+
}, WalkError);
282+
},
283+
);
284+
270285
// https://github.com/denoland/deno_std/issues/1358
271286
testWalk(
272287
async (d: string) => {

0 commit comments

Comments
 (0)