]> git.kaiwu.me - njs.git/commitdiff
Crypto: added back custom exception types using new public API.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 21 Jun 2023 23:29:48 +0000 (16:29 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 21 Jun 2023 23:29:48 +0000 (16:29 -0700)
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.

external/njs_crypto_module.c
src/test/njs_unit_test.c

index ae5d14caaa1cd1af7fba444da2a45fb24e6c685b..58f1787e9a72918bf54aa21ea9c38caa6ffa4585 100644 (file)
@@ -335,7 +335,7 @@ njs_hash_prototype_update(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     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, njs_value_t *args, njs_uint_t nargs,
     } 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, njs_value_t *args, njs_uint_t nargs,
         }
 
     } 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, njs_value_t *args, njs_uint_t nargs,
     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, njs_value_t *args, njs_uint_t nargs,
     } 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, njs_value_t *args, njs_uint_t nargs,
 
     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_value_t *args, njs_uint_t nargs,
         }
 
     } 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_value_t *value)
     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_value_t *value)
         }
     }
 
-    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_value_t *value)
 
     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_value_t *value)
         }
     }
 
-    njs_vm_error(vm, "Unknown digest encoding: \"%V\"", &name);
+    njs_vm_type_error(vm, "Unknown digest encoding: \"%V\"", &name);
 
     return NULL;
 }
index 27ce3a883d3f01c6bc8155f13313548f31d7e487..541c9068ea22df47e6c3168c2259141efccc8223 100644 (file)
@@ -20444,25 +20444,25 @@ static njs_unit_test_t  njs_crypto_module_test[] =
               "'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_module_test[] =
       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_module_test[] =
 
     { 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_test[] =
               "    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") },