Skip to content

Commit 32bf0f6

Browse files
committed
fs: throw errors from fs.symlinkSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bf6ce47 commit 32bf0f6

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lib/fs.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ fs.symlink = function(target, path, type_, callback_) {
12201220
const flags = stringToSymlinkType(type);
12211221
const req = new FSReqWrap();
12221222
req.oncomplete = callback;
1223+
12231224
binding.symlink(preprocessSymlinkDestination(target, type, path),
12241225
pathModule.toNamespacedPath(path), flags, req);
12251226
};
@@ -1234,8 +1235,19 @@ fs.symlinkSync = function(target, path, type) {
12341235
validatePath(target, 'target');
12351236
validatePath(path);
12361237
const flags = stringToSymlinkType(type);
1237-
return binding.symlink(preprocessSymlinkDestination(target, type, path),
1238-
pathModule.toNamespacedPath(path), flags);
1238+
1239+
const ctx = { path: target, dest: path };
1240+
binding.symlink(preprocessSymlinkDestination(target, type, path),
1241+
pathModule.toNamespacedPath(path), flags, undefined, ctx);
1242+
1243+
if (ctx.errno !== undefined) {
1244+
throw new errors.uvException(ctx);
1245+
} else if (ctx.error) {
1246+
// TODO(joyeecheung): this is an encoding error usually caused by memory
1247+
// problems. We need to figure out proper error code(s) for this.
1248+
Error.captureStackTrace(ctx.error);
1249+
throw ctx.error;
1250+
}
12391251
};
12401252

12411253
fs.link = function(existingPath, newPath, callback) {

src/node_file.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,26 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
598598
static void Symlink(const FunctionCallbackInfo<Value>& args) {
599599
Environment* env = Environment::GetCurrent(args);
600600

601-
CHECK_GE(args.Length(), 3);
601+
int argc = args.Length();
602+
CHECK_GE(argc, 4);
602603

603604
BufferValue target(env->isolate(), args[0]);
604605
CHECK_NE(*target, nullptr);
605606
BufferValue path(env->isolate(), args[1]);
606607
CHECK_NE(*path, nullptr);
607608

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

611612
if (args[3]->IsObject()) { // symlink(target, path, flags, req)
612613
CHECK_EQ(args.Length(), 4);
613614
AsyncDestCall(env, args, "symlink", *path, path.length(), UTF8,
614615
AfterNoArgs, uv_fs_symlink, *target, *path, flags);
615-
} else { // symlink(target, path, flags)
616-
SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags)
616+
} else { // symlink(target, path, flags, undefinec, ctx)
617+
CHECK_EQ(argc, 5);
618+
fs_req_wrap req_wrap;
619+
SyncCall(env, args[4], &req_wrap, "symlink",
620+
uv_fs_symlink, *target, *path, flags);
617621
}
618622
}
619623

0 commit comments

Comments
 (0)