]> git.kaiwu.me - njs.git/commitdiff
Added public API to throw standard exceptions.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 21 Jun 2023 23:17:42 +0000 (16:17 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 21 Jun 2023 23:17:42 +0000 (16:17 -0700)
src/njs.h
src/njs_error.c
src/njs_error.h
src/njs_parser.c
src/njs_promise.c
src/njs_vm.c
src/njs_vm.h

index d33d265cc2f37c104ee2a67d016390c2abee5345..accfc7cfcb6159af26f9066dbc4908a331914e00 100644 (file)
--- a/src/njs.h
+++ b/src/njs.h
@@ -72,12 +72,23 @@ extern const njs_value_t            njs_value_undefined;
     ((n < nargs) ? njs_argument(args, n)                                      \
                  : (njs_value_assign(lvalue, &njs_value_undefined), lvalue))
 
-#define njs_vm_log(vm, fmt, ...)  njs_vm_logger(vm, NJS_LOG_LEVEL_INFO, fmt,  \
-                                                ##__VA_ARGS__)
-#define njs_vm_warn(vm, fmt, ...)  njs_vm_logger(vm, NJS_LOG_LEVEL_WARN, fmt, \
-                                                ##__VA_ARGS__)
-#define njs_vm_err(vm, fmt, ...)  njs_vm_logger(vm, NJS_LOG_LEVEL_ERROR, fmt, \
-                                                ##__VA_ARGS__)
+#define njs_vm_log(vm, fmt, ...)                                              \
+    njs_vm_logger(vm, NJS_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
+#define njs_vm_warn(vm, fmt, ...)                                             \
+    njs_vm_logger(vm, NJS_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__)
+#define njs_vm_err(vm, fmt, ...)                                              \
+    njs_vm_logger(vm, NJS_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
+
+#define njs_vm_error(vm, fmt, ...)                                            \
+    njs_vm_error2(vm, 0, fmt, ##__VA_ARGS__)
+#define njs_vm_internal_error(vm, fmt, ...)                                   \
+    njs_vm_error2(vm, 2, fmt, ##__VA_ARGS__)
+#define njs_vm_range_error(vm, fmt, ...)                                      \
+    njs_vm_error2(vm, 3, fmt, ##__VA_ARGS__)
+#define njs_vm_syntax_error(vm, fmt, ...)                                     \
+    njs_vm_error2(vm, 5, fmt, ##__VA_ARGS__)
+#define njs_vm_type_error(vm, fmt, ...)                                       \
+    njs_vm_error2(vm, 6, fmt, ##__VA_ARGS__)
 
 #define njs_deprecated(vm, text)                                             \
     do {                                                                     \
@@ -419,7 +430,8 @@ NJS_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, const njs_str_t *name);
 NJS_EXPORT njs_bool_t njs_vm_constructor(njs_vm_t *vm);
 
 NJS_EXPORT void njs_vm_throw(njs_vm_t *vm, const njs_value_t *value);
-NJS_EXPORT void njs_vm_error(njs_vm_t *vm, const char *fmt, ...);
+NJS_EXPORT void njs_vm_error2(njs_vm_t *vm, unsigned type, const char *fmt,
+    ...);
 NJS_EXPORT void njs_vm_exception_get(njs_vm_t *vm, njs_value_t *retval);
 NJS_EXPORT njs_mp_t *njs_vm_memory_pool(njs_vm_t *vm);
 NJS_EXPORT njs_external_ptr_t njs_vm_external_ptr(njs_vm_t *vm);
index 4cf2130e80e0809d4dba007296450ca27160f2e6..e522e78fe79c3e02fb0387cd870649ad9c46a6bc 100644 (file)
@@ -28,7 +28,7 @@ static const njs_value_t  njs_error_errors_string = njs_string("errors");
 
 
 void
-njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
+njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_t *proto,
     u_char *start, size_t size)
 {
     ssize_t        length;
@@ -46,7 +46,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
         return;
     }
 
-    error = njs_error_alloc(vm, type, NULL, &string, NULL);
+    error = njs_error_alloc(vm, proto, NULL, &string, NULL);
     if (njs_slow_path(error == NULL)) {
         return;
     }
@@ -55,7 +55,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
 }
 
 void
-njs_throw_error_va(njs_vm_t *vm, njs_object_type_t type, const char *fmt,
+njs_throw_error_va(njs_vm_t *vm, njs_object_t *proto, const char *fmt,
     va_list args)
 {
     u_char   buf[NJS_MAX_ERROR_STR], *p;
@@ -66,7 +66,7 @@ njs_throw_error_va(njs_vm_t *vm, njs_object_type_t type, const char *fmt,
         p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args);
     }
 
-    njs_error_new(vm, &vm->exception, type, buf, p - buf);
+    njs_error_new(vm, &vm->exception, proto, buf, p - buf);
 }
 
 
@@ -76,7 +76,7 @@ njs_throw_error(njs_vm_t *vm, njs_object_type_t type, const char *fmt, ...)
     va_list  args;
 
     va_start(args, fmt);
-    njs_throw_error_va(vm, type, fmt, args);
+    njs_throw_error_va(vm, &vm->prototypes[type].object, fmt, args);
     va_end(args);
 }
 
@@ -96,7 +96,7 @@ njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
         va_end(args);
     }
 
-    njs_error_new(vm, dst, type, buf, p - buf);
+    njs_error_new(vm, dst, &vm->prototypes[type].object, buf, p - buf);
 }
 
 
@@ -202,7 +202,7 @@ njs_error_stack(njs_vm_t *vm, njs_value_t *value, njs_value_t *stack)
 
 
 njs_object_t *
-njs_error_alloc(njs_vm_t *vm, njs_object_type_t type, const njs_value_t *name,
+njs_error_alloc(njs_vm_t *vm, njs_object_t *proto, const njs_value_t *name,
     const njs_value_t *message, const njs_value_t *errors)
 {
     njs_int_t           ret;
@@ -223,7 +223,7 @@ njs_error_alloc(njs_vm_t *vm, njs_object_type_t type, const njs_value_t *name,
     error->fast_array = 0;
     error->error_data = 1;
     error->stack_attached = 0;
-    error->__proto__ = &vm->prototypes[type].object;
+    error->__proto__ = proto;
     error->slots = NULL;
 
     lhq.replace = 0;
@@ -339,7 +339,7 @@ njs_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
     }
 
-    error = njs_error_alloc(vm, type, NULL,
+    error = njs_error_alloc(vm, &vm->prototypes[type].object, NULL,
                             njs_is_defined(value) ? value : NULL,
                             njs_is_defined(&list) ? &list : NULL);
     if (njs_slow_path(error == NULL)) {
index e2d6ed854a0029c03ea19b94a70bbf7a1a543700..651b6229f15e4d9f46009af1ed456f03addda5ce 100644 (file)
 #define njs_uri_error(vm, fmt, ...)                                           \
     njs_throw_error(vm, NJS_OBJ_TYPE_URI_ERROR, fmt, ##__VA_ARGS__)
 
-void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
+void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_t *proto,
     u_char *start, size_t size);
 void njs_noinline njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst,
     njs_object_type_t type, const char *fmt, ...);
 void njs_throw_error(njs_vm_t *vm, njs_object_type_t type, const char *fmt,
     ...);
-void njs_throw_error_va(njs_vm_t *vm, njs_object_type_t type, const char *fmt,
+void njs_throw_error_va(njs_vm_t *vm, njs_object_t *proto, const char *fmt,
     va_list args);
 
 void njs_memory_error(njs_vm_t *vm);
 void njs_memory_error_set(njs_vm_t *vm, njs_value_t *value);
 
-njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_object_type_t type,
+njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_object_t *proto,
     const njs_value_t *name, const njs_value_t *message,
     const njs_value_t *errors);
 njs_int_t njs_error_to_string(njs_vm_t *vm, njs_value_t *retval,
index 5a26a84a787a6d1d7eb02bc9c78ddd270d0fa542..35c5bbeab63aa0ae4f99b168d2fdb84d14b34124 100644 (file)
@@ -9205,7 +9205,7 @@ njs_parser_error(njs_vm_t *vm, njs_object_type_t type, njs_str_t *file,
         p = njs_sprintf(p, end, " in %uD", line);
     }
 
-    njs_error_new(vm, &error, type, msg, p - msg);
+    njs_error_new(vm, &error, &vm->prototypes[type].object, msg, p - msg);
 
     njs_set_number(&value, line);
     njs_value_property_set(vm, &error, njs_value_arg(&line_number), &value);
index 871f7fed65933273ad1b733eeb8aab856ddb5a19..6dc3de18d70dda8ff71a39895baacdf416c28a45 100644 (file)
@@ -1351,8 +1351,9 @@ njs_promise_perform_all(njs_vm_t *vm, njs_value_t *iterator,
         njs_set_array(&argument, pargs->args.data);
 
         if (handler == njs_promise_perform_any_handler) {
-            error = njs_error_alloc(vm, NJS_OBJ_TYPE_AGGREGATE_ERROR,
-                                    NULL, &string_any_rejected, &argument);
+            error = njs_error_alloc(vm,
+                           &vm->prototypes[NJS_OBJ_TYPE_AGGREGATE_ERROR].object,
+                           NULL, &string_any_rejected, &argument);
             if (njs_slow_path(error == NULL)) {
                 return NJS_ERROR;
             }
@@ -1728,8 +1729,9 @@ njs_promise_any_reject_element_functions(njs_vm_t *vm, njs_value_t *args,
     if (--(*context->remaining_elements) == 0) {
         njs_mp_free(vm->mem_pool, context->remaining_elements);
 
-        error = njs_error_alloc(vm, NJS_OBJ_TYPE_AGGREGATE_ERROR,
-                                NULL, &string_any_rejected, &arr_value);
+        error = njs_error_alloc(vm,
+                          &vm->prototypes[NJS_OBJ_TYPE_AGGREGATE_ERROR].object,
+                          NULL, &string_any_rejected, &arr_value);
         if (njs_slow_path(error == NULL)) {
             return NJS_ERROR;
         }
index dfbe2f137590c2419ed93691a223d2727151cadc..b29cd4037f624a81e4d46f211bce33519b5b6354 100644 (file)
@@ -733,12 +733,17 @@ njs_vm_throw(njs_vm_t *vm, const njs_value_t *value)
 
 
 void
-njs_vm_error(njs_vm_t *vm, const char *fmt, ...)
+njs_vm_error2(njs_vm_t *vm, unsigned type, const char *fmt, ...)
 {
     va_list  args;
 
+    if (type > (NJS_OBJ_TYPE_ERROR_MAX - NJS_OBJ_TYPE_ERROR)) {
+        return;
+    }
+
     va_start(args, fmt);
-    njs_throw_error_va(vm, NJS_OBJ_TYPE_ERROR, fmt, args);
+    type += NJS_OBJ_TYPE_ERROR;
+    njs_throw_error_va(vm, &vm->prototypes[type].object, fmt, args);
     va_end(args);
 }
 
index 314f0eacdf56b8a207d5b2980b9272d8dfc60773..b1ecb7e20be659f4ca94fcb4fbffc938a776d5fd 100644 (file)
@@ -76,6 +76,7 @@ typedef enum {
     NJS_OBJ_TYPE_URI_ERROR,
     NJS_OBJ_TYPE_MEMORY_ERROR,
     NJS_OBJ_TYPE_AGGREGATE_ERROR,
+#define NJS_OBJ_TYPE_ERROR_MAX         (NJS_OBJ_TYPE_AGGREGATE_ERROR)
 
     NJS_OBJ_TYPE_MAX,
 } njs_object_type_t;