@@ -11,6 +11,20 @@ import {
11
11
WalkEntry ,
12
12
} from "./_util.ts" ;
13
13
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
+
14
28
function include (
15
29
path : string ,
16
30
exts ?: string [ ] ,
@@ -29,16 +43,9 @@ function include(
29
43
return true ;
30
44
}
31
45
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 ) ;
42
49
}
43
50
44
51
export interface WalkOptions {
@@ -124,7 +131,7 @@ export async function* walk(
124
131
}
125
132
}
126
133
} catch ( err ) {
127
- throw wrapErrorWithRootPath ( err , normalize ( root ) ) ;
134
+ throw wrapErrorWithPath ( err , normalize ( root ) ) ;
128
135
}
129
136
}
130
137
@@ -155,7 +162,7 @@ export function* walkSync(
155
162
try {
156
163
entries = Deno . readDirSync ( root ) ;
157
164
} catch ( err ) {
158
- throw wrapErrorWithRootPath ( err , normalize ( root ) ) ;
165
+ throw wrapErrorWithPath ( err , normalize ( root ) ) ;
159
166
}
160
167
for ( const entry of entries ) {
161
168
assert ( entry . name != null ) ;
0 commit comments