[njs] Introduced fs.Error.code.

Dmitry Volyntsev xeioex at nginx.com
Mon May 25 14:23:53 UTC 2020


details:   https://hg.nginx.org/njs/rev/0287ae53cbb1
branches:  
changeset: 1396:0287ae53cbb1
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Mon May 11 09:58:28 2020 +0300
description:
Introduced fs.Error.code.

diffstat:

 src/njs_fs.c               |  16 ++++++++++++++++
 test/js/fs_promises_002.js |   9 +++++----
 test/js/fs_promises_004.js |   6 +++---
 test/njs_expect_test.exp   |   6 +++---
 4 files changed, 27 insertions(+), 10 deletions(-)

diffs (128 lines):

diff -r 6c46a9004ad8 -r 0287ae53cbb1 src/njs_fs.c
--- a/src/njs_fs.c	Mon May 11 09:58:28 2020 +0300
+++ b/src/njs_fs.c	Mon May 11 09:58:28 2020 +0300
@@ -873,9 +873,11 @@ njs_fs_error(njs_vm_t *vm, const char *s
     size_t        size;
     njs_int_t     ret;
     njs_value_t   value;
+    const char    *code;
     njs_object_t  *error;
 
     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 +902,20 @@ 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 6c46a9004ad8 -r 0287ae53cbb1 test/js/fs_promises_002.js
--- a/test/js/fs_promises_002.js	Mon May 11 09:58:28 2020 +0300
+++ 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 6c46a9004ad8 -r 0287ae53cbb1 test/js/fs_promises_004.js
--- a/test/js/fs_promises_004.js	Mon May 11 09:58:28 2020 +0300
+++ 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 6c46a9004ad8 -r 0287ae53cbb1 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Mon May 11 09:58:28 2020 +0300
+++ 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()


More information about the nginx-devel mailing list