diff --git a/lib/fs.js b/lib/fs.js index 299b42f6a57b68..e6ba189fe81309 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1649,7 +1649,10 @@ fs.realpathSync.native = function(path, options) { options = getOptions(options, {}); path = getPathFromURL(path); validatePath(path); - return binding.realpath(path, options.encoding); + const ctx = { path }; + const result = binding.realpath(path, options.encoding, undefined, ctx); + handleErrorFromBinding(ctx); + return result; }; diff --git a/src/node_file.cc b/src/node_file.cc index ec2e35c25bc40e..e3c9801b8a845c 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1047,20 +1047,30 @@ static void MKDir(const FunctionCallbackInfo& args) { } static void RealPath(const FunctionCallbackInfo& args) { - CHECK_GE(args.Length(), 2); Environment* env = Environment::GetCurrent(args); + + const int argc = args.Length(); + CHECK_GE(argc, 3); + BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); FSReqBase* req_wrap = GetReqWrap(env, args[2]); - if (req_wrap != nullptr) { + if (req_wrap != nullptr) { // realpath(path, encoding, req) AsyncCall(env, req_wrap, args, "realpath", encoding, AfterStringPtr, uv_fs_realpath, *path); - } else { - SYNC_CALL(realpath, *path, *path); - const char* link_path = static_cast(SYNC_REQ.ptr); + } else { // realpath(path, encoding, undefined, ctx) + CHECK_EQ(argc, 4); + fs_req_wrap req_wrap; + int err = SyncCall(env, args[3], &req_wrap, "realpath", + uv_fs_realpath, *path); + if (err < 0) { + return; // syscall failed, no need to continue, error info is in ctx + } + + const char* link_path = static_cast(req_wrap.req.ptr); Local error; MaybeLocal rc = StringBytes::Encode(env->isolate(), @@ -1068,9 +1078,11 @@ static void RealPath(const FunctionCallbackInfo& args) { encoding, &error); if (rc.IsEmpty()) { - env->isolate()->ThrowException(error); + Local ctx = args[3].As(); + ctx->Set(env->context(), env->error_string(), error).FromJust(); return; } + args.GetReturnValue().Set(rc.ToLocalChecked()); } } diff --git a/test/parallel/test-fs-error-messages.js b/test/parallel/test-fs-error-messages.js index 28461de9bee9b2..fad7802ce12f8b 100644 --- a/test/parallel/test-fs-error-messages.js +++ b/test/parallel/test-fs-error-messages.js @@ -125,6 +125,27 @@ function re(literals, ...values) { ); } +// native realpath +{ + const validateError = (err) => { + assert.strictEqual(nonexistentFile, err.path); + assert.strictEqual( + err.message, + `ENOENT: no such file or directory, realpath '${nonexistentFile}'`); + assert.strictEqual(err.errno, uv.UV_ENOENT); + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.syscall, 'realpath'); + return true; + }; + + fs.realpath.native(nonexistentFile, common.mustCall(validateError)); + + assert.throws( + () => fs.realpathSync.native(nonexistentFile), + validateError + ); +} + // readlink { const validateError = (err) => {