Skip to content

Instantly share code, notes, and snippets.

@drsm
Created May 11, 2020 07:00
Show Gist options
  • Save drsm/23a3167637465b48ddb0b82fc5481516 to your computer and use it in GitHub Desktop.
Save drsm/23a3167637465b48ddb0b82fc5481516 to your computer and use it in GitHub Desktop.
# HG changeset patch
# User Artem S. Povalyukhin <artem.povaluhin@gmail.com>
# Date 1589180308 -10800
# Mon May 11 09:58:28 2020 +0300
# Node ID 3f2c209222753066505f35b994fea3600311c2fb
# Parent 92838d964b19da7fc4080dc944b5fa4aa7c4e6a1
Introduced fs.Error.code.
diff -r 92838d964b19 -r 3f2c20922275 src/njs_fs.c
--- a/src/njs_fs.c Fri May 08 15:52:40 2020 +0000
+++ b/src/njs_fs.c Mon May 11 09:58:28 2020 +0300
@@ -866,6 +866,340 @@ njs_fs_encoding(njs_vm_t *vm, njs_value_
}
+/*
+ * Copyright Node.js contributors. All rights reserved.
+ */
+static const char* njs_errno_string(int errorno) {
+#define ERRNO_CASE(e) \
+ case e: \
+ return #e;
+ switch (errorno) {
+#ifdef EACCES
+ ERRNO_CASE(EACCES);
+#endif
+
+#ifdef EADDRINUSE
+ ERRNO_CASE(EADDRINUSE);
+#endif
+
+#ifdef EADDRNOTAVAIL
+ ERRNO_CASE(EADDRNOTAVAIL);
+#endif
+
+#ifdef EAFNOSUPPORT
+ ERRNO_CASE(EAFNOSUPPORT);
+#endif
+
+#ifdef EAGAIN
+ ERRNO_CASE(EAGAIN);
+#endif
+
+#ifdef EWOULDBLOCK
+#if EAGAIN != EWOULDBLOCK
+ ERRNO_CASE(EWOULDBLOCK);
+#endif
+#endif
+
+#ifdef EALREADY
+ ERRNO_CASE(EALREADY);
+#endif
+
+#ifdef EBADF
+ ERRNO_CASE(EBADF);
+#endif
+
+#ifdef EBADMSG
+ ERRNO_CASE(EBADMSG);
+#endif
+
+#ifdef EBUSY
+ ERRNO_CASE(EBUSY);
+#endif
+
+#ifdef ECANCELED
+ ERRNO_CASE(ECANCELED);
+#endif
+
+#ifdef ECHILD
+ ERRNO_CASE(ECHILD);
+#endif
+
+#ifdef ECONNABORTED
+ ERRNO_CASE(ECONNABORTED);
+#endif
+
+#ifdef ECONNREFUSED
+ ERRNO_CASE(ECONNREFUSED);
+#endif
+
+#ifdef ECONNRESET
+ ERRNO_CASE(ECONNRESET);
+#endif
+
+#ifdef EDEADLK
+ ERRNO_CASE(EDEADLK);
+#endif
+
+#ifdef EDESTADDRREQ
+ ERRNO_CASE(EDESTADDRREQ);
+#endif
+
+#ifdef EDOM
+ ERRNO_CASE(EDOM);
+#endif
+
+#ifdef EDQUOT
+ ERRNO_CASE(EDQUOT);
+#endif
+
+#ifdef EEXIST
+ ERRNO_CASE(EEXIST);
+#endif
+
+#ifdef EFAULT
+ ERRNO_CASE(EFAULT);
+#endif
+
+#ifdef EFBIG
+ ERRNO_CASE(EFBIG);
+#endif
+
+#ifdef EHOSTUNREACH
+ ERRNO_CASE(EHOSTUNREACH);
+#endif
+
+#ifdef EIDRM
+ ERRNO_CASE(EIDRM);
+#endif
+
+#ifdef EILSEQ
+ ERRNO_CASE(EILSEQ);
+#endif
+
+#ifdef EINPROGRESS
+ ERRNO_CASE(EINPROGRESS);
+#endif
+
+#ifdef EINTR
+ ERRNO_CASE(EINTR);
+#endif
+
+#ifdef EINVAL
+ ERRNO_CASE(EINVAL);
+#endif
+
+#ifdef EIO
+ ERRNO_CASE(EIO);
+#endif
+
+#ifdef EISCONN
+ ERRNO_CASE(EISCONN);
+#endif
+
+#ifdef EISDIR
+ ERRNO_CASE(EISDIR);
+#endif
+
+#ifdef ELOOP
+ ERRNO_CASE(ELOOP);
+#endif
+
+#ifdef EMFILE
+ ERRNO_CASE(EMFILE);
+#endif
+
+#ifdef EMLINK
+ ERRNO_CASE(EMLINK);
+#endif
+
+#ifdef EMSGSIZE
+ ERRNO_CASE(EMSGSIZE);
+#endif
+
+#ifdef EMULTIHOP
+ ERRNO_CASE(EMULTIHOP);
+#endif
+
+#ifdef ENAMETOOLONG
+ ERRNO_CASE(ENAMETOOLONG);
+#endif
+
+#ifdef ENETDOWN
+ ERRNO_CASE(ENETDOWN);
+#endif
+
+#ifdef ENETRESET
+ ERRNO_CASE(ENETRESET);
+#endif
+
+#ifdef ENETUNREACH
+ ERRNO_CASE(ENETUNREACH);
+#endif
+
+#ifdef ENFILE
+ ERRNO_CASE(ENFILE);
+#endif
+
+#ifdef ENOBUFS
+ ERRNO_CASE(ENOBUFS);
+#endif
+
+#ifdef ENODATA
+ ERRNO_CASE(ENODATA);
+#endif
+
+#ifdef ENODEV
+ ERRNO_CASE(ENODEV);
+#endif
+
+#ifdef ENOENT
+ ERRNO_CASE(ENOENT);
+#endif
+
+#ifdef ENOEXEC
+ ERRNO_CASE(ENOEXEC);
+#endif
+
+#ifdef ENOLINK
+ ERRNO_CASE(ENOLINK);
+#endif
+
+#ifdef ENOLCK
+#if ENOLINK != ENOLCK
+ ERRNO_CASE(ENOLCK);
+#endif
+#endif
+
+#ifdef ENOMEM
+ ERRNO_CASE(ENOMEM);
+#endif
+
+#ifdef ENOMSG
+ ERRNO_CASE(ENOMSG);
+#endif
+
+#ifdef ENOPROTOOPT
+ ERRNO_CASE(ENOPROTOOPT);
+#endif
+
+#ifdef ENOSPC
+ ERRNO_CASE(ENOSPC);
+#endif
+
+#ifdef ENOSR
+ ERRNO_CASE(ENOSR);
+#endif
+
+#ifdef ENOSTR
+ ERRNO_CASE(ENOSTR);
+#endif
+
+#ifdef ENOSYS
+ ERRNO_CASE(ENOSYS);
+#endif
+
+#ifdef ENOTCONN
+ ERRNO_CASE(ENOTCONN);
+#endif
+
+#ifdef ENOTDIR
+ ERRNO_CASE(ENOTDIR);
+#endif
+
+#ifdef ENOTEMPTY
+#if ENOTEMPTY != EEXIST
+ ERRNO_CASE(ENOTEMPTY);
+#endif
+#endif
+
+#ifdef ENOTSOCK
+ ERRNO_CASE(ENOTSOCK);
+#endif
+
+#ifdef ENOTSUP
+ ERRNO_CASE(ENOTSUP);
+#else
+#ifdef EOPNOTSUPP
+ ERRNO_CASE(EOPNOTSUPP);
+#endif
+#endif
+
+#ifdef ENOTTY
+ ERRNO_CASE(ENOTTY);
+#endif
+
+#ifdef ENXIO
+ ERRNO_CASE(ENXIO);
+#endif
+
+#ifdef EOVERFLOW
+ ERRNO_CASE(EOVERFLOW);
+#endif
+
+#ifdef EPERM
+ ERRNO_CASE(EPERM);
+#endif
+
+#ifdef EPIPE
+ ERRNO_CASE(EPIPE);
+#endif
+
+#ifdef EPROTO
+ ERRNO_CASE(EPROTO);
+#endif
+
+#ifdef EPROTONOSUPPORT
+ ERRNO_CASE(EPROTONOSUPPORT);
+#endif
+
+#ifdef EPROTOTYPE
+ ERRNO_CASE(EPROTOTYPE);
+#endif
+
+#ifdef ERANGE
+ ERRNO_CASE(ERANGE);
+#endif
+
+#ifdef EROFS
+ ERRNO_CASE(EROFS);
+#endif
+
+#ifdef ESPIPE
+ ERRNO_CASE(ESPIPE);
+#endif
+
+#ifdef ESRCH
+ ERRNO_CASE(ESRCH);
+#endif
+
+#ifdef ESTALE
+ ERRNO_CASE(ESTALE);
+#endif
+
+#ifdef ETIME
+ ERRNO_CASE(ETIME);
+#endif
+
+#ifdef ETIMEDOUT
+ ERRNO_CASE(ETIMEDOUT);
+#endif
+
+#ifdef ETXTBSY
+ ERRNO_CASE(ETXTBSY);
+#endif
+
+#ifdef EXDEV
+ ERRNO_CASE(EXDEV);
+#endif
+
+#undef ERRNO_CASE
+
+ default:
+ return "";
+ }
+}
+
+
static njs_int_t
njs_fs_error(njs_vm_t *vm, const char *syscall, const char *description,
njs_value_t *path, int errn, njs_value_t *retval)
@@ -874,8 +1208,10 @@ njs_fs_error(njs_vm_t *vm, const char *s
njs_int_t ret;
njs_value_t value;
njs_object_t *error;
+ const char *code;
static const njs_value_t string_errno = njs_string("errno");
+ static const njs_value_t string_code = njs_string("code");
static const njs_value_t string_path = njs_string("path");
static const njs_value_t string_syscall = njs_string("syscall");
@@ -900,6 +1236,19 @@ njs_fs_error(njs_vm_t *vm, const char *s
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
+
+ code = njs_errno_string(errn);
+ size = njs_strlen(code);
+ ret = njs_string_new(vm, &value, (u_char *) code, size, size);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NJS_ERROR;
+ }
+
+ ret = njs_value_property_set(vm, retval, njs_value_arg(&string_code),
+ &value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NJS_ERROR;
+ }
}
if (path != NULL) {
diff -r 92838d964b19 -r 3f2c20922275 test/js/fs_promises_002.js
--- a/test/js/fs_promises_002.js Fri May 08 15:52:40 2020 +0000
+++ b/test/js/fs_promises_002.js Mon May 11 09:58:28 2020 +0300
@@ -14,8 +14,7 @@ var testSync = new Promise((resolve, rej
fs.accessSync(fname + '___');
failed = true;
} catch(e) {
- failed = (e.syscall != 'access');
- // TODO: e.code != 'ENOENT'
+ failed = (e.syscall != 'access') || e.code != 'ENOENT';
}
resolve(failed);
} catch (e) {
@@ -33,7 +32,8 @@ var testCallback = new Promise((resolve,
fs.access(fname, fs.constants.R_OK | fs.constants.W_OK, (err) => {
failed |= (err !== undefined);
fs.access(fname + '___', (err) => {
- failed |= ((err === undefined) || (err.syscall != 'access'));
+ failed |= ((err === undefined) || (err.syscall != 'access')
+ || err.code != 'ENOENT');
resolve(failed);
});
});
@@ -66,6 +66,7 @@ Promise.resolve()
console.log('testPromise failed');
})
.catch((e) => {
- console.log('testPromise ok', (e.syscall == 'access') && (e.path == fname + '___'));
+ console.log('testPromise ok', (e.syscall == 'access') && (e.path == fname + '___')
+ && e.code == 'ENOENT');
})
;
diff -r 92838d964b19 -r 3f2c20922275 test/js/fs_promises_004.js
--- a/test/js/fs_promises_004.js Fri May 08 15:52:40 2020 +0000
+++ b/test/js/fs_promises_004.js Mon May 11 09:58:28 2020 +0300
@@ -23,7 +23,7 @@ var testSync = () => new Promise((resolv
fs.realpathSync(fname);
throw new Error('fs.realpathSync error 1');
} catch (e) {
- if (e.syscall != 'realpath') { // e.code
+ if (e.syscall != 'realpath' || e.code != 'ENOENT') {
throw e;
}
}
@@ -79,7 +79,7 @@ var testCallback = () => new Promise((re
reject(new Error('fs.realpath error 1'));
return;
}
- if (err.syscall != 'realpath') {
+ if (err.syscall != 'realpath' || err.code != 'ENOENT') {
reject(err);
return;
}
@@ -165,7 +165,7 @@ Promise.resolve()
.then(() => fsp.realpath(fname)
.then(() => { throw new Error('fsp.realpath error 1') }))
.catch((e) => {
- if (e.syscall != 'realpath') {
+ if (e.syscall != 'realpath' || e.code != 'ENOENT') {
throw e;
}
})
diff -r 92838d964b19 -r 3f2c20922275 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp Fri May 08 15:52:40 2020 +0000
+++ b/test/njs_expect_test.exp Mon May 11 09:58:28 2020 +0300
@@ -511,7 +511,7 @@ njs_test {
{"var fs = require('fs'); \r\n"
"undefined\r\n>> "}
{"fs.readFile('test/fs/nonexistent', 'utf8', function (e) {console.log(JSON.stringify(e))})\r\n"
- "undefined\r\n{\"errno\":2,\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}\r\n>> "}
+ "undefined\r\n{\"errno\":2,\"code\":\"ENOENT\",\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}\r\n>> "}
}
njs_test {
@@ -571,7 +571,7 @@ njs_test {
{"var fs = require('fs'); \r\n"
"undefined\r\n>> "}
{"try { fs.readFileSync('test/fs/nonexistent')} catch (e) {console.log(JSON.stringify(e))}\r\n"
- "{\"errno\":2,\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}\r\nundefined\r\n>> "}
+ "{\"errno\":2,\"code\":\"ENOENT\",\"path\":\"test/fs/nonexistent\",\"syscall\":\"open\"}\r\nundefined\r\n>> "}
}
njs_test {
@@ -648,7 +648,7 @@ njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.writeFile('/invalid_path', 'ABC', function (e) { console.log(JSON.stringify(e))})\r\n"
- "undefined\r\n{\"errno\":13,\"path\":\"/invalid_path\",\"syscall\":\"open\"}\r\n>> "}
+ "undefined\r\n{\"errno\":13,\"code\":\"EACCES\",\"path\":\"/invalid_path\",\"syscall\":\"open\"}\r\n>> "}
}
# require('fs').writeFileSync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment