summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2017-11-27 13:08:05 +0900
committerJoyee Cheung <joyeec9h3@gmail.com>2017-12-28 03:08:25 +0800
commit6ca10de9468ed027f5e0b45f721d441df5972bc9 (patch)
tree52cafdbc2d1fb85f1267ec733d88cc31eac4fa73 /lib
parentc56972779b8c239a48df46b7caadf1ef1b9eacd2 (diff)
downloadandroid-node-v8-6ca10de9468ed027f5e0b45f721d441df5972bc9.tar.gz
android-node-v8-6ca10de9468ed027f5e0b45f721d441df5972bc9.tar.bz2
android-node-v8-6ca10de9468ed027f5e0b45f721d441df5972bc9.zip
fs: simplify the error context collection in C++
- Simplify the SyncCall template function, only collect error number and syscall in the C++ layer and collect the rest of context in JS for flexibility. - Remove the stringFromPath JS helper now that the unprefixed path is directly put into the context before the binding is invoked with the prefixed path. - Validate more properties in fs.access tests. PR-URL: https://github.com/nodejs/node/pull/17338 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.js4
-rw-r--r--lib/internal/errors.js35
2 files changed, 16 insertions, 23 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 93be375334..f357ff2b9d 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -329,10 +329,10 @@ fs.accessSync = function(path, mode) {
else
mode = mode | 0;
- const ctx = {};
+ const ctx = { path };
binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx);
- if (ctx.code !== undefined) {
+ if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
};
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 9e3d1a9355..db501a1d3c 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -14,6 +14,7 @@ const kCode = Symbol('code');
const kInfo = Symbol('info');
const messages = new Map();
+const { errmap } = process.binding('uv');
const { kMaxLength } = process.binding('buffer');
const { defineProperty } = Object;
@@ -194,43 +195,35 @@ function E(sym, val) {
messages.set(sym, typeof val === 'function' ? val : String(val));
}
-// JS counterpart of StringFromPath, although here path is a buffer.
-function stringFromPath(path) {
- const str = path.toString();
- if (process.platform !== 'win32') {
- return str;
- }
-
- if (str.startsWith('\\\\?\\UNC\\')) {
- return '\\\\' + str.slice(8);
- } else if (str.startsWith('\\\\?\\')) {
- return str.slice(4);
- }
- return str;
-}
-
// This creates an error compatible with errors produced in UVException
// using the context collected in CollectUVExceptionInfo
// The goal is to migrate them to ERR_* errors later when
// compatibility is not a concern
function uvException(ctx) {
const err = new Error();
- err.errno = ctx.errno;
- err.code = ctx.code;
- err.syscall = ctx.syscall;
- let message = `${ctx.code}: ${ctx.message}, ${ctx.syscall}`;
+ for (const prop of Object.keys(ctx)) {
+ if (prop === 'message' || prop === 'path' || prop === 'dest') {
+ continue;
+ }
+ err[prop] = ctx[prop];
+ }
+
+ const [ code, uvmsg ] = errmap.get(ctx.errno);
+ err.code = code;
+ let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
if (ctx.path) {
- const path = stringFromPath(ctx.path);
+ const path = ctx.path.toString();
message += ` '${path}'`;
err.path = path;
}
if (ctx.dest) {
- const dest = stringFromPath(ctx.dest);
+ const dest = ctx.dest.toString();
message += ` -> '${dest}'`;
err.dest = dest;
}
err.message = message;
+
Error.captureStackTrace(err, uvException);
return err;
}