Skip to content

Commit 0c23b0b

Browse files
committed
process: wrap ENOENT from process.cwd() with ERR_SYSTEM_ERROR
Catch ENOENT from rawMethods.cwd() and rethrow as ERR_SYSTEM_ERROR with context { syscall: 'process.cwd', code: 'ENOENT' }, preserving the original error in cause. This surfaces a clearer, actionable message when the current working directory has been deleted or unmounted and improves the stack (process.cwd instead of wrappedCwd). Fixes: #57045
1 parent f1a8f44 commit 0c23b0b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lib/internal/bootstrap/switches/does_own_process_state.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ const {
3535
validateString,
3636
validateUint32,
3737
} = require('internal/validators');
38+
const {
39+
codes: {
40+
ERR_INVALID_ARG_TYPE,
41+
ERR_SYSTEM_ERROR,
42+
ERR_UNKNOWN_CREDENTIAL,
43+
},
44+
} = require('internal/errors');
3845

3946
function wrapPosixCredentialSetters(credentials) {
40-
const {
41-
codes: {
42-
ERR_INVALID_ARG_TYPE,
43-
ERR_UNKNOWN_CREDENTIAL,
44-
},
45-
} = require('internal/errors');
46-
4747
const {
4848
initgroups: _initgroups,
4949
setgroups: _setgroups,
@@ -138,7 +138,24 @@ function wrappedUmask(mask) {
138138
}
139139

140140
function wrappedCwd() {
141-
if (cachedCwd === '')
142-
cachedCwd = rawMethods.cwd();
141+
if (cachedCwd === '') {
142+
try {
143+
cachedCwd = rawMethods.cwd();
144+
} catch (err) {
145+
if (err.code === 'ENOENT') {
146+
const context = {
147+
code: 'ENOENT',
148+
syscall: 'process.cwd',
149+
message:
150+
'The current working directory does not exist. It may have been deleted or unmounted.',
151+
};
152+
const wrapped = new ERR_SYSTEM_ERROR(context);
153+
// Preserve the original error for debugging.
154+
wrapped.cause = err;
155+
throw wrapped;
156+
}
157+
throw err;
158+
}
159+
}
143160
return cachedCwd;
144161
}

0 commit comments

Comments
 (0)