Skip to content

Commit b2e20b0

Browse files
committed
fs: extract binding error handling into a helper
PR-URL: #18642 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 18d23aa commit b2e20b0

File tree

1 file changed

+32
-60
lines changed

1 file changed

+32
-60
lines changed

lib/fs.js

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ function copyObject(source) {
108108
return target;
109109
}
110110

111+
function handleErrorFromBinding(ctx) {
112+
if (ctx.errno !== undefined) { // libuv error numbers
113+
const err = errors.uvException(ctx);
114+
Error.captureStackTrace(err, handleErrorFromBinding);
115+
throw err;
116+
} else if (ctx.error !== undefined) { // errors created in C++ land.
117+
// TODO(joyeecheung): currently, ctx.error are encoding errors
118+
// usually caused by memory problems. We need to figure out proper error
119+
// code(s) for this.
120+
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
121+
throw ctx.error;
122+
}
123+
}
124+
111125
// TODO(joyeecheung): explore how the deprecation could be solved via linting
112126
// rules. See https://github.com/nodejs/node/pull/12976
113127
function rethrow() {
@@ -405,10 +419,7 @@ fs.accessSync = function(path, mode) {
405419

406420
const ctx = { path };
407421
binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx);
408-
409-
if (ctx.errno !== undefined) {
410-
throw errors.uvException(ctx);
411-
}
422+
handleErrorFromBinding(ctx);
412423
};
413424

414425
// fs.exists never throws even when the arguments are invalid - if there is
@@ -742,9 +753,7 @@ fs.closeSync = function(fd) {
742753

743754
const ctx = {};
744755
binding.close(fd, undefined, ctx);
745-
if (ctx.errno !== undefined) {
746-
throw errors.uvException(ctx);
747-
}
756+
handleErrorFromBinding(ctx);
748757
};
749758

750759
function modeNum(m, def) {
@@ -924,9 +933,7 @@ fs.renameSync = function(oldPath, newPath) {
924933
const ctx = { path: oldPath, dest: newPath };
925934
binding.rename(pathModule.toNamespacedPath(oldPath),
926935
pathModule.toNamespacedPath(newPath), undefined, ctx);
927-
if (ctx.errno !== undefined) {
928-
throw errors.uvException(ctx);
929-
}
936+
handleErrorFromBinding(ctx);
930937
};
931938

932939
fs.truncate = function(path, len, callback) {
@@ -994,9 +1001,7 @@ fs.ftruncateSync = function(fd, len = 0) {
9941001
len = Math.max(0, len);
9951002
const ctx = {};
9961003
binding.ftruncate(fd, len, undefined, ctx);
997-
if (ctx.errno !== undefined) {
998-
throw errors.uvException(ctx);
999-
}
1004+
handleErrorFromBinding(ctx);
10001005
};
10011006

10021007
fs.rmdir = function(path, callback) {
@@ -1025,9 +1030,7 @@ fs.fdatasyncSync = function(fd) {
10251030
validateUint32(fd, 'fd');
10261031
const ctx = {};
10271032
binding.fdatasync(fd, undefined, ctx);
1028-
if (ctx.errno !== undefined) {
1029-
throw errors.uvException(ctx);
1030-
}
1033+
handleErrorFromBinding(ctx);
10311034
};
10321035

10331036
fs.fsync = function(fd, callback) {
@@ -1041,9 +1044,7 @@ fs.fsyncSync = function(fd) {
10411044
validateUint32(fd, 'fd');
10421045
const ctx = {};
10431046
binding.fsync(fd, undefined, ctx);
1044-
if (ctx.errno !== undefined) {
1045-
throw errors.uvException(ctx);
1046-
}
1047+
handleErrorFromBinding(ctx);
10471048
};
10481049

10491050
fs.mkdir = function(path, mode, callback) {
@@ -1114,9 +1115,7 @@ fs.fstatSync = function(fd) {
11141115
validateUint32(fd, 'fd');
11151116
const ctx = { fd };
11161117
binding.fstat(fd, undefined, ctx);
1117-
if (ctx.errno !== undefined) {
1118-
throw errors.uvException(ctx);
1119-
}
1118+
handleErrorFromBinding(ctx);
11201119
return statsFromValues();
11211120
};
11221121

@@ -1125,9 +1124,7 @@ fs.lstatSync = function(path) {
11251124
validatePath(path);
11261125
const ctx = { path };
11271126
binding.lstat(pathModule.toNamespacedPath(path), undefined, ctx);
1128-
if (ctx.errno !== undefined) {
1129-
throw errors.uvException(ctx);
1130-
}
1127+
handleErrorFromBinding(ctx);
11311128
return statsFromValues();
11321129
};
11331130

@@ -1136,9 +1133,7 @@ fs.statSync = function(path) {
11361133
validatePath(path);
11371134
const ctx = { path };
11381135
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
1139-
if (ctx.errno !== undefined) {
1140-
throw errors.uvException(ctx);
1141-
}
1136+
handleErrorFromBinding(ctx);
11421137
return statsFromValues();
11431138
};
11441139

@@ -1159,14 +1154,7 @@ fs.readlinkSync = function(path, options) {
11591154
const ctx = { path };
11601155
const result = binding.readlink(pathModule.toNamespacedPath(path),
11611156
options.encoding, undefined, ctx);
1162-
if (ctx.errno !== undefined) {
1163-
throw errors.uvException(ctx);
1164-
} else if (ctx.error) {
1165-
// TODO(joyeecheung): this is an encoding error usually caused by memory
1166-
// problems. We need to figure out proper error code(s) for this.
1167-
Error.captureStackTrace(ctx.error);
1168-
throw ctx.error;
1169-
}
1157+
handleErrorFromBinding(ctx);
11701158
return result;
11711159
};
11721160

@@ -1235,9 +1223,7 @@ fs.symlinkSync = function(target, path, type) {
12351223
binding.symlink(preprocessSymlinkDestination(target, type, path),
12361224
pathModule.toNamespacedPath(path), flags, undefined, ctx);
12371225

1238-
if (ctx.errno !== undefined) {
1239-
throw errors.uvException(ctx);
1240-
}
1226+
handleErrorFromBinding(ctx);
12411227
};
12421228

12431229
fs.link = function(existingPath, newPath, callback) {
@@ -1266,9 +1252,7 @@ fs.linkSync = function(existingPath, newPath) {
12661252
const result = binding.link(pathModule.toNamespacedPath(existingPath),
12671253
pathModule.toNamespacedPath(newPath),
12681254
undefined, ctx);
1269-
if (ctx.errno !== undefined) {
1270-
throw errors.uvException(ctx);
1271-
}
1255+
handleErrorFromBinding(ctx);
12721256
return result;
12731257
};
12741258

@@ -1286,9 +1270,7 @@ fs.unlinkSync = function(path) {
12861270
validatePath(path);
12871271
const ctx = { path };
12881272
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
1289-
if (ctx.errno !== undefined) {
1290-
throw errors.uvException(ctx);
1291-
}
1273+
handleErrorFromBinding(ctx);
12921274
};
12931275

12941276
fs.fchmod = function(fd, mode, callback) {
@@ -1887,9 +1869,7 @@ fs.realpathSync = function realpathSync(p, options) {
18871869
if (isWindows && !knownHard[base]) {
18881870
const ctx = { path: base };
18891871
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
1890-
if (ctx.errno !== undefined) {
1891-
throw errors.uvException(ctx);
1892-
}
1872+
handleErrorFromBinding(ctx);
18931873
knownHard[base] = true;
18941874
}
18951875

@@ -1931,9 +1911,7 @@ fs.realpathSync = function realpathSync(p, options) {
19311911
var baseLong = pathModule.toNamespacedPath(base);
19321912
const ctx = { path: base };
19331913
binding.lstat(baseLong, undefined, ctx);
1934-
if (ctx.errno !== undefined) {
1935-
throw errors.uvException(ctx);
1936-
}
1914+
handleErrorFromBinding(ctx);
19371915

19381916
if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
19391917
knownHard[base] = true;
@@ -1956,13 +1934,9 @@ fs.realpathSync = function realpathSync(p, options) {
19561934
if (linkTarget === null) {
19571935
const ctx = { path: base };
19581936
binding.stat(baseLong, undefined, ctx);
1959-
if (ctx.errno !== undefined) {
1960-
throw errors.uvException(ctx);
1961-
}
1937+
handleErrorFromBinding(ctx);
19621938
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
1963-
if (ctx.errno !== undefined) {
1964-
throw errors.uvException(ctx);
1965-
}
1939+
handleErrorFromBinding(ctx);
19661940
}
19671941
resolvedLink = pathModule.resolve(previous, linkTarget);
19681942

@@ -1981,9 +1955,7 @@ fs.realpathSync = function realpathSync(p, options) {
19811955
if (isWindows && !knownHard[base]) {
19821956
const ctx = { path: base };
19831957
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
1984-
if (ctx.errno !== undefined) {
1985-
throw errors.uvException(ctx);
1986-
}
1958+
handleErrorFromBinding(ctx);
19871959
knownHard[base] = true;
19881960
}
19891961
}

0 commit comments

Comments
 (0)