Skip to content

Commit 77b42e3

Browse files
committed
fs: throw mkdirSync errors in JS
PR-URL: #18871 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 46164ba commit 77b42e3

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ fs.mkdirSync = function(path, mode) {
799799
validatePath(path);
800800
mode = modeNum(mode, 0o777);
801801
validateUint32(mode, 'mode');
802-
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
802+
const ctx = { path };
803+
binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx);
804+
handleErrorFromBinding(ctx);
803805
};
804806

805807
fs.readdir = function(path, options, callback) {

src/node_file.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,20 +1025,24 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
10251025
static void MKDir(const FunctionCallbackInfo<Value>& args) {
10261026
Environment* env = Environment::GetCurrent(args);
10271027

1028-
CHECK_GE(args.Length(), 2);
1029-
CHECK(args[1]->IsInt32());
1028+
const int argc = args.Length();
1029+
CHECK_GE(argc, 3);
10301030

10311031
BufferValue path(env->isolate(), args[0]);
10321032
CHECK_NE(*path, nullptr);
10331033

1034-
int mode = static_cast<int>(args[1]->Int32Value());
1034+
CHECK(args[1]->IsInt32());
1035+
const int mode = args[1].As<Int32>()->Value();
10351036

10361037
FSReqBase* req_wrap = GetReqWrap(env, args[2]);
1037-
if (req_wrap != nullptr) {
1038+
if (req_wrap != nullptr) { // mkdir(path, mode, req)
10381039
AsyncCall(env, req_wrap, args, "mkdir", UTF8, AfterNoArgs,
10391040
uv_fs_mkdir, *path, mode);
1040-
} else {
1041-
SYNC_CALL(mkdir, *path, *path, mode)
1041+
} else { // mkdir(path, mode, undefined, ctx)
1042+
CHECK_EQ(argc, 4);
1043+
fs_req_wrap req_wrap;
1044+
SyncCall(env, args[3], &req_wrap, "mkdir",
1045+
uv_fs_mkdir, *path, mode);
10421046
}
10431047
}
10441048

0 commit comments

Comments
 (0)