From 776f6cdfc4d7f8b3e0eaff6bd71571ebd937609b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 24 Jan 2018 22:18:37 +0800 Subject: [PATCH] fs: throw errors from fs.unlinkSync 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/18348 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell --- lib/fs.js | 6 +++++- src/node_file.cc | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index e3801d30db2cec..d4f5ee0909bcef 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1328,7 +1328,11 @@ fs.unlinkSync = function(path) { handleError((path = getPathFromURL(path))); nullCheck(path); validatePath(path); - return binding.unlink(pathModule.toNamespacedPath(path)); + const ctx = { path }; + binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } }; fs.fchmod = function(fd, mode, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 3ffec4225b216c..93f54aed23f728 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -777,17 +777,21 @@ static void Fsync(const FunctionCallbackInfo& args) { static void Unlink(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK_GE(args.Length(), 1); + const int argc = args.Length(); + CHECK_GE(argc, 2); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); - if (args[1]->IsObject()) { - CHECK_EQ(args.Length(), 2); + if (args[1]->IsObject()) { // unlink(fd, req) + CHECK_EQ(argc, 2); AsyncCall(env, args, "unlink", UTF8, AfterNoArgs, uv_fs_unlink, *path); - } else { - SYNC_CALL(unlink, *path, *path) + } else { // unlink(fd, undefined, ctx) + CHECK_EQ(argc, 3); + fs_req_wrap req_wrap; + SyncCall(env, args[2], &req_wrap, "unlink", + uv_fs_unlink, *path); } }