From 77b42e34de519d211f7b68330781af67be76fd35 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 3 Feb 2018 21:49:32 +0800 Subject: [PATCH] fs: throw mkdirSync errors in JS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18871 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/fs.js | 4 +++- src/node_file.cc | 16 ++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 72770f371a9..299b42f6a57 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -799,7 +799,9 @@ fs.mkdirSync = function(path, mode) { validatePath(path); mode = modeNum(mode, 0o777); validateUint32(mode, 'mode'); - return binding.mkdir(pathModule.toNamespacedPath(path), mode); + const ctx = { path }; + binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx); + handleErrorFromBinding(ctx); }; fs.readdir = function(path, options, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 5aa82a6a90c..ec2e35c25bc 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1025,20 +1025,24 @@ static void RMDir(const FunctionCallbackInfo& args) { static void MKDir(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK_GE(args.Length(), 2); - CHECK(args[1]->IsInt32()); + const int argc = args.Length(); + CHECK_GE(argc, 3); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); - int mode = static_cast(args[1]->Int32Value()); + CHECK(args[1]->IsInt32()); + const int mode = args[1].As()->Value(); FSReqBase* req_wrap = GetReqWrap(env, args[2]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // mkdir(path, mode, req) AsyncCall(env, req_wrap, args, "mkdir", UTF8, AfterNoArgs, uv_fs_mkdir, *path, mode); - } else { - SYNC_CALL(mkdir, *path, *path, mode) + } else { // mkdir(path, mode, undefined, ctx) + CHECK_EQ(argc, 4); + fs_req_wrap req_wrap; + SyncCall(env, args[3], &req_wrap, "mkdir", + uv_fs_mkdir, *path, mode); } }