summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2016-11-17 22:03:39 -0500
committerBrian White <mscdex@mscdex.net>2016-12-14 03:29:33 -0500
commitf3cf8e98081b52003bfe3c10f27f29c1c49e800a (patch)
tree747f439ca9f9d1b35053df02e55b675da40cfc2c
parent4f97a146ff2f44c9385b83a6b519b3336b2433be (diff)
downloadnode-new-f3cf8e98081b52003bfe3c10f27f29c1c49e800a.tar.gz
fs: do not pass Buffer when toString() fails
Even though an Error object is passed to the callback when readFile() fails due to toString() failing, it is a bit strange to still see data passed as the second argument. This commit changes that and only passes the Error object in that case. PR-URL: https://github.com/nodejs/node/pull/9670 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--lib/fs.js11
-rw-r--r--test/parallel/test-fs-readfile-tostring-fail.js1
2 files changed, 5 insertions, 7 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 27614e5cf1..7aafc170d7 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -396,8 +396,8 @@ function readFileAfterClose(err) {
var buffer = null;
var callback = context.callback;
- if (context.err)
- return callback(context.err);
+ if (context.err || err)
+ return callback(context.err || err);
if (context.size === 0)
buffer = Buffer.concat(context.buffers, context.pos);
@@ -406,8 +406,6 @@ function readFileAfterClose(err) {
else
buffer = context.buffer;
- if (err) return callback(err, buffer);
-
if (context.encoding) {
return tryToString(buffer, context.encoding, callback);
}
@@ -416,13 +414,12 @@ function readFileAfterClose(err) {
}
function tryToString(buf, encoding, callback) {
- var e = null;
try {
buf = buf.toString(encoding);
} catch (err) {
- e = err;
+ return callback(err);
}
- callback(e, buf);
+ callback(null, buf);
}
function tryStatSync(fd, isUserFd) {
diff --git a/test/parallel/test-fs-readfile-tostring-fail.js b/test/parallel/test-fs-readfile-tostring-fail.js
index 8ed9658a25..f3d728f57f 100644
--- a/test/parallel/test-fs-readfile-tostring-fail.js
+++ b/test/parallel/test-fs-readfile-tostring-fail.js
@@ -33,6 +33,7 @@ stream.on('finish', common.mustCall(function() {
fs.readFile(file, 'utf8', common.mustCall(function(err, buf) {
assert.ok(err instanceof Error);
assert.strictEqual('"toString()" failed', err.message);
+ assert.strictEqual(buf, undefined);
}));
}));