Skip to content

Commit d07eb3b

Browse files
authored
fix(utils): statPath: do not throw on EACCES (#506)
1 parent 2239609 commit d07eb3b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/mongodb-memory-server-core/src/util/__tests__/utils.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('utils', () => {
3131
});
3232

3333
describe('statPath', () => {
34-
it('should throw an error if code is not "ENOENT"', async () => {
34+
it('should throw an error if code is not "ENOENT" or "EACCES"', async () => {
3535
const retError = new Error();
3636
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
3737
retError.code = 'EPERM';
@@ -97,7 +97,20 @@ describe('utils', () => {
9797
).resolves.toBeUndefined();
9898
});
9999

100-
it('should throw an error if error is not "ENOENT"', async () => {
100+
it('should return "undefined" if "EACCES"', async () => {
101+
const retError = new Error();
102+
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
103+
retError.code = 'EACCES';
104+
jest.spyOn(fspromises, 'readFile').mockRejectedValueOnce(retError);
105+
106+
await expect(
107+
utils.tryReleaseFile('/some/path', () => {
108+
fail('This Function should never run');
109+
})
110+
).resolves.toBeUndefined();
111+
});
112+
113+
it('should throw an error if error is not "ENOENT" or "EACCES"', async () => {
101114
const retError = new Error();
102115
// @ts-expect-error because there is no FSError, or an error with an "code" property - but still being used
103116
retError.code = 'EPERM';

packages/mongodb-memory-server-core/src/util/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,16 @@ export function authDefault(opts: AutomaticAuth): Required<AutomaticAuth> {
145145
}
146146

147147
/**
148-
* Run "fs.promises.stat", but return "undefined" if error is "ENOENT"
148+
* Run "fs.promises.stat", but return "undefined" if error is "ENOENT" or "EACCES"
149149
* follows symlinks
150150
* @param path The Path to Stat
151-
* @throws if the error is not "ENOENT"
151+
* @throws if the error is not "ENOENT" or "EACCES"
152152
*/
153153
export async function statPath(path: string): Promise<Stats | undefined> {
154154
return fspromises.stat(path).catch((err) => {
155-
if (err.code === 'ENOENT') {
156-
return undefined; // catch the error if the directory dosnt exist, without throwing an error
155+
// catch the error if the directory doesn't exist or permission is denied, without throwing an error
156+
if (['ENOENT', 'EACCES'].includes(err.code)) {
157+
return undefined;
157158
}
158159

159160
throw err;
@@ -184,7 +185,7 @@ export async function tryReleaseFile(
184185

185186
return parser(output.toString());
186187
} catch (err) {
187-
if (err.code !== 'ENOENT') {
188+
if (!['ENOENT', 'EACCES'].includes(err.code)) {
188189
throw err;
189190
}
190191

0 commit comments

Comments
 (0)