]> git.kaiwu.me - njs.git/commitdiff
Fetch: generalize ngx_js_http_error().
authorZhidao HONG <z.hong@f5.com>
Fri, 11 Apr 2025 13:47:52 +0000 (21:47 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Fri, 11 Apr 2025 15:28:45 +0000 (23:28 +0800)
Refactored to support both njs and QuickJS, which have different error
formats when throwing exceptions.

nginx/ngx_js_fetch.c

index a803ab4c8b67b004cdcfa8b05c3db8d0191bc330..2db73904d55c47219fb9ad92acb15ff82572e984 100644 (file)
@@ -156,17 +156,6 @@ struct ngx_js_http_s {
 };
 
 
-
-
-#define ngx_js_http_error(http, fmt, ...)                                     \
-    do {                                                                      \
-        njs_vm_error((http)->vm, fmt, ##__VA_ARGS__);                         \
-        njs_vm_exception_get((http)->vm,                                      \
-                             njs_value_arg(&(http)->response_value));         \
-        ngx_js_http_fetch_done(http, &(http)->response_value, NJS_ERROR);     \
-    } while (0)
-
-
 static njs_int_t ngx_js_method_process(njs_vm_t *vm, ngx_js_request_t *r);
 static njs_int_t ngx_js_headers_inherit(njs_vm_t *vm, ngx_js_headers_t *headers,
     ngx_js_headers_t *orig);
@@ -1330,6 +1319,26 @@ failed:
 }
 
 
+static void
+ngx_js_http_error(ngx_js_http_t *http, const char *fmt, ...)
+{
+    u_char   *p, *end;
+    va_list   args;
+    u_char    err[NGX_MAX_ERROR_STR];
+
+    end = err + NGX_MAX_ERROR_STR - 1;
+
+    va_start(args, fmt);
+    p = njs_vsprintf(err, end, fmt, args);
+    *p = '\0';
+    va_end(args);
+
+    njs_vm_error(http->vm, (const char *) err);
+    njs_vm_exception_get(http->vm, njs_value_arg(&http->response_value));
+    ngx_js_http_fetch_done(http, &http->response_value, NJS_ERROR);
+}
+
+
 static void
 ngx_js_resolve_handler(ngx_resolver_ctx_t *ctx)
 {