[njs] Crypto: added back custom exception types using new public API.

Dmitry Volyntsev xeioex at nginx.com
Thu Jun 22 22:40:04 UTC 2023


details:   https://hg.nginx.org/njs/rev/11fc41439e9f
branches:  
changeset: 2159:11fc41439e9f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Jun 21 16:29:48 2023 -0700
description:
Crypto: added back custom exception types using new public API.

In b2cbf06ba017, when rewriting crypto module using public API all the
exceptions types were squashed into a single Error type. This patch
reintroduces the standard exception types back using new API.

diffstat:

 external/njs_crypto_module.c |  22 +++++++++++-----------
 src/test/njs_unit_test.c     |  30 +++++++++++++++---------------
 2 files changed, 26 insertions(+), 26 deletions(-)

diffs (196 lines):

diff -r aaab085e499f -r 11fc41439e9f external/njs_crypto_module.c
--- a/external/njs_crypto_module.c	Wed Jun 21 16:29:45 2023 -0700
+++ b/external/njs_crypto_module.c	Wed Jun 21 16:29:48 2023 -0700
@@ -335,7 +335,7 @@ njs_hash_prototype_update(njs_vm_t *vm, 
     if (!hmac) {
         dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, this);
         if (njs_slow_path(dgst == NULL)) {
-            njs_vm_error(vm, "\"this\" is not a hash object");
+            njs_vm_type_error(vm, "\"this\" is not a hash object");
             return NJS_ERROR;
         }
 
@@ -349,7 +349,7 @@ njs_hash_prototype_update(njs_vm_t *vm, 
     } else {
         ctx = njs_vm_external(vm, njs_crypto_hmac_proto_id, this);
         if (njs_slow_path(ctx == NULL)) {
-            njs_vm_error(vm, "\"this\" is not a hmac object");
+            njs_vm_type_error(vm, "\"this\" is not a hmac object");
             return NJS_ERROR;
         }
 
@@ -383,7 +383,7 @@ njs_hash_prototype_update(njs_vm_t *vm, 
         }
 
     } else {
-        njs_vm_error(vm, "data is not a string or Buffer-like object");
+        njs_vm_type_error(vm, "data is not a string or Buffer-like object");
 
         return NJS_ERROR;
     }
@@ -418,7 +418,7 @@ njs_hash_prototype_digest(njs_vm_t *vm, 
     if (!hmac) {
         dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, this);
         if (njs_slow_path(dgst == NULL)) {
-            njs_vm_error(vm, "\"this\" is not a hash object");
+            njs_vm_type_error(vm, "\"this\" is not a hash object");
             return NJS_ERROR;
         }
 
@@ -431,7 +431,7 @@ njs_hash_prototype_digest(njs_vm_t *vm, 
     } else {
         ctx = njs_vm_external(vm, njs_crypto_hmac_proto_id, this);
         if (njs_slow_path(ctx == NULL)) {
-            njs_vm_error(vm, "\"this\" is not a hmac object");
+            njs_vm_type_error(vm, "\"this\" is not a hmac object");
             return NJS_ERROR;
         }
 
@@ -484,7 +484,7 @@ njs_hash_prototype_copy(njs_vm_t *vm, nj
 
     dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, njs_argument(args, 0));
     if (njs_slow_path(dgst == NULL)) {
-        njs_vm_error(vm, "\"this\" is not a hash object");
+        njs_vm_type_error(vm, "\"this\" is not a hash object");
         return NJS_ERROR;
     }
 
@@ -547,7 +547,7 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
         }
 
     } else {
-        njs_vm_error(vm, "key is not a string or Buffer-like object");
+        njs_vm_type_error(vm, "key is not a string or Buffer-like object");
 
         return NJS_ERROR;
     }
@@ -597,7 +597,7 @@ njs_crypto_algorithm(njs_vm_t *vm, njs_v
     njs_hash_alg_t  *e;
 
     if (njs_slow_path(!njs_value_is_string(value))) {
-        njs_vm_error(vm, "algorithm must be a string");
+        njs_vm_type_error(vm, "algorithm must be a string");
         return NULL;
     }
 
@@ -609,7 +609,7 @@ njs_crypto_algorithm(njs_vm_t *vm, njs_v
         }
     }
 
-    njs_vm_error(vm, "not supported algorithm: \"%V\"", &name);
+    njs_vm_type_error(vm, "not supported algorithm: \"%V\"", &name);
 
     return NULL;
 }
@@ -623,7 +623,7 @@ njs_crypto_encoding(njs_vm_t *vm, njs_va
 
     if (njs_slow_path(!njs_value_is_string(value))) {
         if (!njs_value_is_undefined(value)) {
-            njs_vm_error(vm, "encoding must be a string");
+            njs_vm_type_error(vm, "encoding must be a string");
             return NULL;
         }
 
@@ -638,7 +638,7 @@ njs_crypto_encoding(njs_vm_t *vm, njs_va
         }
     }
 
-    njs_vm_error(vm, "Unknown digest encoding: \"%V\"", &name);
+    njs_vm_type_error(vm, "Unknown digest encoding: \"%V\"", &name);
 
     return NULL;
 }
diff -r aaab085e499f -r 11fc41439e9f src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Jun 21 16:29:45 2023 -0700
+++ b/src/test/njs_unit_test.c	Wed Jun 21 16:29:48 2023 -0700
@@ -20444,25 +20444,25 @@ static njs_unit_test_t  njs_crypto_modul
               "'d9f5aeb06abebb3be3f38adec9a2e3b94228d52193be923eb4e24c9b56ee0930']]") },
 
     { njs_str("var h = require('crypto').createHash()"),
-      njs_str("Error: algorithm must be a string") },
+      njs_str("TypeError: algorithm must be a string") },
 
     { njs_str("var h = require('crypto').createHash([])"),
-      njs_str("Error: algorithm must be a string") },
+      njs_str("TypeError: algorithm must be a string") },
 
     { njs_str("var h = require('crypto').createHash('sha512')"),
-      njs_str("Error: not supported algorithm: \"sha512\"") },
+      njs_str("TypeError: not supported algorithm: \"sha512\"") },
 
     { njs_str("var h = require('crypto').createHash('sha1');"
               "h.update()"),
-      njs_str("Error: data is not a string or Buffer-like object") },
+      njs_str("TypeError: data is not a string or Buffer-like object") },
 
     { njs_str("var h = require('crypto').createHash('sha1');"
               "h.update({})"),
-      njs_str("Error: data is not a string or Buffer-like object") },
+      njs_str("TypeError: data is not a string or Buffer-like object") },
 
     { njs_str("var h = require('crypto').createHash('sha1');"
               "h.update('A').digest('latin1')"),
-      njs_str("Error: Unknown digest encoding: \"latin1\"") },
+      njs_str("TypeError: Unknown digest encoding: \"latin1\"") },
 
     { njs_str("require('crypto').createHash('sha1').digest() instanceof Buffer"),
       njs_str("true") },
@@ -20562,16 +20562,16 @@ static njs_unit_test_t  njs_crypto_modul
       njs_str("5647b6c429701ff512f0f18232b4507065d2376ca8899a816a0a6e721bf8ddcc") },
 
     { njs_str("var h = require('crypto').createHmac()"),
-      njs_str("Error: algorithm must be a string") },
+      njs_str("TypeError: algorithm must be a string") },
 
     { njs_str("var h = require('crypto').createHmac([])"),
-      njs_str("Error: algorithm must be a string") },
+      njs_str("TypeError: algorithm must be a string") },
 
     { njs_str("var h = require('crypto').createHmac('sha512', '')"),
-      njs_str("Error: not supported algorithm: \"sha512\"") },
+      njs_str("TypeError: not supported algorithm: \"sha512\"") },
 
     { njs_str("var h = require('crypto').createHmac('sha1', [])"),
-      njs_str("Error: key is not a string or Buffer-like object") },
+      njs_str("TypeError: key is not a string or Buffer-like object") },
 
     { njs_str("var h = require('crypto').createHmac('sha1', 'secret key');"
               "h.update('A').digest('hex'); h.digest('hex')"),
@@ -20586,7 +20586,7 @@ static njs_unit_test_t  njs_crypto_modul
 
     { njs_str("var cr = require('crypto'); var h = cr.createHash('sha1');"
               "h.update.call(cr.createHmac('sha1', 's'), '')"),
-      njs_str("Error: \"this\" is not a hash object") },
+      njs_str("TypeError: \"this\" is not a hash object") },
 };
 
 static njs_unit_test_t  njs_querystring_module_test[] =
@@ -23279,24 +23279,24 @@ static njs_unit_test_t  njs_backtraces_t
               "    at main (:1)\n") },
 
     { njs_str("require('crypto').createHash('sha')"),
-      njs_str("Error: not supported algorithm: \"sha\"\n"
+      njs_str("TypeError: not supported algorithm: \"sha\"\n"
               "    at crypto.createHash (native)\n"
               "    at main (:1)\n") },
 
     { njs_str("var h = require('crypto').createHash('sha1');"
               "h.update([])"),
-      njs_str("Error: data is not a string or Buffer-like object\n"
+      njs_str("TypeError: data is not a string or Buffer-like object\n"
               "    at Hash.update (native)\n"
               "    at main (:1)\n") },
 
     { njs_str("require('crypto').createHmac('sha1', [])"),
-      njs_str("Error: key is not a string or Buffer-like object\n"
+      njs_str("TypeError: key is not a string or Buffer-like object\n"
               "    at crypto.createHmac (native)\n"
               "    at main (:1)\n") },
 
     { njs_str("var h = require('crypto').createHmac('sha1', 'secret');"
               "h.update([])"),
-      njs_str("Error: data is not a string or Buffer-like object\n"
+      njs_str("TypeError: data is not a string or Buffer-like object\n"
               "    at Hmac.update (native)\n"
               "    at main (:1)\n") },
 


More information about the nginx-devel mailing list