Skip to content

Commit

Permalink
fs: throw errors from fs.symlinkSync in JS
Browse files Browse the repository at this point in the history
PR-URL: #18348
Refs: #18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Feb 1, 2018
1 parent bf6ce47 commit 32bf0f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ fs.symlink = function(target, path, type_, callback_) {
const flags = stringToSymlinkType(type);
const req = new FSReqWrap();
req.oncomplete = callback;

binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags, req);
};
Expand All @@ -1234,8 +1235,19 @@ fs.symlinkSync = function(target, path, type) {
validatePath(target, 'target');
validatePath(path);
const flags = stringToSymlinkType(type);
return binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags);

const ctx = { path: target, dest: path };
binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags, undefined, ctx);

if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
} else if (ctx.error) {
// TODO(joyeecheung): this is an encoding error usually caused by memory
// problems. We need to figure out proper error code(s) for this.
Error.captureStackTrace(ctx.error);
throw ctx.error;
}
};

fs.link = function(existingPath, newPath, callback) {
Expand Down
14 changes: 9 additions & 5 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -598,22 +598,26 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
static void Symlink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 3);
int argc = args.Length();
CHECK_GE(argc, 4);

BufferValue target(env->isolate(), args[0]);
CHECK_NE(*target, nullptr);
BufferValue path(env->isolate(), args[1]);
CHECK_NE(*path, nullptr);

CHECK(args[2]->IsUint32());
int flags = args[2]->Uint32Value(env->context()).ToChecked();
CHECK(args[2]->IsInt32());
int flags = args[2].As<Int32>()->Value();

if (args[3]->IsObject()) { // symlink(target, path, flags, req)
CHECK_EQ(args.Length(), 4);
AsyncDestCall(env, args, "symlink", *path, path.length(), UTF8,
AfterNoArgs, uv_fs_symlink, *target, *path, flags);
} else { // symlink(target, path, flags)
SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags)
} else { // symlink(target, path, flags, undefinec, ctx)
CHECK_EQ(argc, 5);
fs_req_wrap req_wrap;
SyncCall(env, args[4], &req_wrap, "symlink",
uv_fs_symlink, *target, *path, flags);
}
}

Expand Down

0 comments on commit 32bf0f6

Please sign in to comment.