fs: throw realpathSync.native errors in JS

PR-URL: https://github.com/nodejs/node/pull/18871
Refs: https://github.com/nodejs/node/issues/18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2018-02-03 22:22:05 +08:00
parent 77b42e34de
commit 72d150ea6f
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
3 changed files with 43 additions and 7 deletions

View File

@ -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;
};

View File

@ -1047,20 +1047,30 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
}
static void RealPath(const FunctionCallbackInfo<Value>& 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<const char*>(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<const char*>(req_wrap.req.ptr);
Local<Value> error;
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
@ -1068,9 +1078,11 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
encoding,
&error);
if (rc.IsEmpty()) {
env->isolate()->ThrowException(error);
Local<Object> ctx = args[3].As<Object>();
ctx->Set(env->context(), env->error_string(), error).FromJust();
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
}
}

View File

@ -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) => {