Skip to content

Commit fea5dda

Browse files
committed
fs: throw openSync errors in JS
PR-URL: #18871 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent d2c4f50 commit fea5dda

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/fs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,12 @@ fs.openSync = function(path, flags, mode) {
521521
validatePath(path);
522522
validateUint32(mode, 'mode');
523523

524-
return binding.open(pathModule.toNamespacedPath(path),
525-
stringToFlags(flags), mode);
524+
const ctx = { path };
525+
const result = binding.open(pathModule.toNamespacedPath(path),
526+
stringToFlags(flags), mode,
527+
undefined, ctx);
528+
handleErrorFromBinding(ctx);
529+
return result;
526530
};
527531

528532
fs.read = function(fd, buffer, offset, length, position, callback) {

src/node_file.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,25 +1169,29 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
11691169

11701170
static void Open(const FunctionCallbackInfo<Value>& args) {
11711171
Environment* env = Environment::GetCurrent(args);
1172-
Local<Context> context = env->context();
11731172

1174-
CHECK_GE(args.Length(), 3);
1175-
CHECK(args[1]->IsInt32());
1176-
CHECK(args[2]->IsInt32());
1173+
const int argc = args.Length();
1174+
CHECK_GE(argc, 3);
11771175

11781176
BufferValue path(env->isolate(), args[0]);
11791177
CHECK_NE(*path, nullptr);
11801178

1181-
int flags = args[1]->Int32Value(context).ToChecked();
1182-
int mode = args[2]->Int32Value(context).ToChecked();
1179+
CHECK(args[1]->IsInt32());
1180+
const int flags = args[1].As<Int32>()->Value();
1181+
1182+
CHECK(args[2]->IsInt32());
1183+
const int mode = args[2].As<Int32>()->Value();
11831184

11841185
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1185-
if (req_wrap != nullptr) {
1186+
if (req_wrap != nullptr) { // open(path, flags, mode, req)
11861187
AsyncCall(env, req_wrap, args, "open", UTF8, AfterInteger,
11871188
uv_fs_open, *path, flags, mode);
1188-
} else {
1189-
SYNC_CALL(open, *path, *path, flags, mode)
1190-
args.GetReturnValue().Set(SYNC_RESULT);
1189+
} else { // open(path, flags, mode, undefined, ctx)
1190+
CHECK_EQ(argc, 5);
1191+
fs_req_wrap req_wrap;
1192+
int result = SyncCall(env, args[4], &req_wrap, "open",
1193+
uv_fs_open, *path, flags, mode);
1194+
args.GetReturnValue().Set(result);
11911195
}
11921196
}
11931197

0 commit comments

Comments
 (0)