Skip to content

Commit fa21607

Browse files
committed
feat: suppress eacces option
1 parent 096a5b6 commit fa21607

File tree

7 files changed

+55
-4
lines changed

7 files changed

+55
-4
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This package provides methods for traversing the file system and returning pathn
3333
* [fs](#fs)
3434
* [ignore](#ignore)
3535
* [suppressErrors](#suppresserrors)
36+
* [suppressEacces](#suppresseacces)
3637
* [throwErrorOnBrokenSymbolicLink](#throwerroronbrokensymboliclink)
3738
* [signal](#signal)
3839
* [Output control](#output-control)
@@ -397,6 +398,13 @@ By default this package suppress only `ENOENT` errors. Set to `true` to suppress
397398

398399
> :book: Can be useful when the directory has entries with a special level of access.
399400
401+
#### suppressEacces
402+
403+
* Type: `boolean`
404+
* Default: `false`
405+
406+
By default, the package throws `EACCES` if a path cannot be accessed due to permission erros. Set to `true` to suppress these errors.
407+
400408
#### throwErrorOnBrokenSymbolicLink
401409

402410
* Type: `boolean`

src/providers/filters/error.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,13 @@ describe('Providers → Filters → Error', () => {
5252

5353
assert.ok(!actual);
5454
});
55+
56+
it('should return false for EACCES error', () => {
57+
const filter = getFilter();
58+
59+
const actual = filter(tests.errno.getEacces());
60+
61+
assert.ok(!actual);
62+
});
5563
});
5664
});

src/providers/filters/error.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export default class ErrorFilter {
1515
}
1616

1717
#isNonFatalError(error: ErrnoException): boolean {
18-
return utils.errno.isEnoentCodeError(error) || this.#settings.suppressErrors;
18+
if (this.#settings.suppressErrors) {
19+
return true;
20+
}
21+
22+
if (this.#settings.errorHandler !== undefined) {
23+
this.#settings.errorHandler(error);
24+
return true;
25+
}
26+
27+
if (utils.errno.isEnoentCodeError(error)) {
28+
return true;
29+
}
30+
31+
return false;
1932
}
2033
}

src/readers/reader.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export abstract class Reader<T> {
4444
}
4545

4646
protected _isFatalError(error: ErrnoException): boolean {
47-
return !utils.errno.isEnoentCodeError(error) && !this.#settings.suppressErrors;
47+
if (this.#settings.suppressErrors) {
48+
return false;
49+
}
50+
51+
if (utils.errno.isEnoentCodeError(error)) {
52+
return false;
53+
}
54+
55+
return true;
4856
}
4957
}

src/settings.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('Settings', () => {
1818
assert.ok(!settings.onlyDirectories);
1919
assert.ok(!settings.stats);
2020
assert.ok(!settings.suppressErrors);
21+
assert.ok(!settings.errorHandler);
2122
assert.ok(!settings.throwErrorOnBrokenSymbolicLink);
2223
assert.ok(settings.braceExpansion);
2324
assert.ok(settings.caseSensitiveMatch);

src/settings.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'node:fs';
22

3-
import type { FileSystemAdapter, Pattern } from './types';
3+
import type { ErrnoException, FileSystemAdapter, Pattern } from './types';
44

55
export const DEFAULT_FILE_SYSTEM_ADAPTER: FileSystemAdapter = {
66
lstat: fs.lstat,
@@ -125,6 +125,12 @@ export interface Options {
125125
* @default false
126126
*/
127127
suppressErrors?: boolean;
128+
/**
129+
* Callback for user-defined error handling. Ignored if `suppressErrors` is `true`.
130+
*
131+
* @default false
132+
*/
133+
errorHandler?: (error: Error) => void;
128134
/**
129135
* Throw an error when symbolic link is broken if `true` or safely
130136
* return `lstat` call if `false`.
@@ -166,6 +172,7 @@ export default class Settings {
166172
public readonly onlyFiles: boolean;
167173
public readonly stats: boolean;
168174
public readonly suppressErrors: boolean;
175+
public readonly errorHandler: ((error: ErrnoException) => void) | undefined;
169176
public readonly throwErrorOnBrokenSymbolicLink: boolean;
170177
public readonly unique: boolean;
171178
public readonly signal?: AbortSignal;
@@ -190,7 +197,9 @@ export default class Settings {
190197
this.onlyFiles = options.onlyFiles ?? true;
191198
this.stats = options.stats ?? false;
192199
this.suppressErrors = options.suppressErrors ?? false;
193-
this.throwErrorOnBrokenSymbolicLink = options.throwErrorOnBrokenSymbolicLink ?? false;
200+
this.errorHandler = options.errorHandler ?? undefined;
201+
this.throwErrorOnBrokenSymbolicLink =
202+
options.throwErrorOnBrokenSymbolicLink ?? false;
194203
this.unique = options.unique ?? true;
195204
this.signal = options.signal;
196205

src/tests/utils/errno.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ export function getEnoent(): ErrnoException {
1414
export function getEperm(): ErrnoException {
1515
return new SystemError('EPERM', 'operation not permitted');
1616
}
17+
18+
export function getEacces(): ErrnoException {
19+
return new SystemError('EACCES', 'permission denied');
20+
}

0 commit comments

Comments
 (0)