Skip to content

Commit

Permalink
fs: throw realpathSync.native errors in JS
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18871
Refs: nodejs#18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
joyeecheung authored and MayaLekova committed May 8, 2018
1 parent fef4153 commit ecd9898
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down
24 changes: 18 additions & 6 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,30 +1047,42 @@ 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(),
link_path,
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());
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit ecd9898

Please sign in to comment.