Skip to content

Commit 70f867e

Browse files
Modules: added error handling when logging js exception string.
1 parent 82a588c commit 70f867e

File tree

3 files changed

+36
-82
lines changed

3 files changed

+36
-82
lines changed

nginx/ngx_js.c

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@ ngx_engine_t *
661661
ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
662662
{
663663
njs_vm_t *vm;
664-
ngx_str_t exception;
665664
ngx_engine_t *engine;
666665
njs_opaque_value_t retval;
667666

@@ -681,9 +680,7 @@ ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
681680
engine->u.njs.vm = vm;
682681

683682
if (njs_vm_start(vm, njs_value_arg(&retval)) == NJS_ERROR) {
684-
ngx_js_exception(vm, &exception);
685-
686-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js exception: %V", &exception);
683+
ngx_js_log_exception(vm, ctx->log, "exception");
687684

688685
njs_vm_destroy(vm);
689686

@@ -701,7 +698,6 @@ ngx_engine_njs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
701698
njs_vm_t *vm;
702699
njs_int_t ret;
703700
njs_str_t name;
704-
ngx_str_t exception;
705701
njs_function_t *func;
706702

707703
name.start = fname->data;
@@ -719,10 +715,7 @@ ngx_engine_njs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
719715
ret = njs_vm_invoke(vm, func, njs_value_arg(args), nargs,
720716
njs_value_arg(&ctx->retval));
721717
if (ret == NJS_ERROR) {
722-
ngx_js_exception(vm, &exception);
723-
724-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
725-
"js exception: %V", &exception);
718+
ngx_js_log_exception(vm, ctx->log, "exception");
726719

727720
return NGX_ERROR;
728721
}
@@ -731,10 +724,8 @@ ngx_engine_njs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
731724
ret = njs_vm_execute_pending_job(vm);
732725
if (ret <= NJS_OK) {
733726
if (ret == NJS_ERROR) {
734-
ngx_js_exception(vm, &exception);
727+
ngx_js_log_exception(vm, ctx->log, "exception");
735728

736-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
737-
"js job exception: %V", &exception);
738729
return NGX_ERROR;
739730
}
740731

@@ -779,7 +770,6 @@ static void
779770
ngx_engine_njs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
780771
ngx_js_loc_conf_t *conf)
781772
{
782-
ngx_str_t exception;
783773
ngx_js_event_t *event;
784774
njs_rbtree_node_t *node;
785775

@@ -798,9 +788,7 @@ ngx_engine_njs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
798788
}
799789

800790
if (ngx_js_unhandled_rejection(ctx)) {
801-
ngx_js_exception(e->u.njs.vm, &exception);
802-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
803-
"js unhandled rejection: %V", &exception);
791+
ngx_js_log_exception(e->u.njs.vm, ctx->log, "unhandled rejection");
804792
}
805793
}
806794

@@ -848,7 +836,6 @@ ngx_engine_qjs_compile(ngx_js_loc_conf_t *conf, ngx_log_t *log, u_char *start,
848836
size_t size)
849837
{
850838
JSValue code;
851-
ngx_str_t text;
852839
JSContext *cx;
853840
ngx_engine_t *engine;
854841
ngx_js_code_entry_t *pc;
@@ -860,8 +847,7 @@ ngx_engine_qjs_compile(ngx_js_loc_conf_t *conf, ngx_log_t *log, u_char *start,
860847
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
861848

862849
if (JS_IsException(code)) {
863-
ngx_qjs_exception(engine, &text);
864-
ngx_log_error(NGX_LOG_EMERG, log, 0, "js compile %V", &text);
850+
ngx_qjs_log_exception(engine, log, "compile");
865851
return NGX_ERROR;
866852
}
867853

@@ -928,7 +914,6 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
928914
njs_mp_t *mp;
929915
uint32_t i, length;
930916
JSRuntime *rt;
931-
ngx_str_t exception;
932917
JSContext *cx;
933918
ngx_engine_t *engine;
934919
ngx_js_code_entry_t *pc;
@@ -983,10 +968,7 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
983968
rv = JS_ReadObject(cx, pc[i].code, pc[i].code_size,
984969
JS_READ_OBJ_BYTECODE);
985970
if (JS_IsException(rv)) {
986-
ngx_qjs_exception(engine, &exception);
987-
988-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
989-
"js load module exception: %V", &exception);
971+
ngx_qjs_log_exception(engine, ctx->log, "load module exception");
990972
goto destroy;
991973
}
992974

@@ -1004,19 +986,13 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
1004986
rv = JS_EvalFunction(cx, rv);
1005987

1006988
if (JS_IsException(rv)) {
1007-
ngx_qjs_exception(engine, &exception);
1008-
1009-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js eval exception: %V",
1010-
&exception);
989+
ngx_qjs_log_exception(engine, ctx->log, "eval exception");
1011990
goto destroy;
1012991
}
1013992

1014993
rv = js_std_await(cx, rv);
1015994
if (JS_IsException(rv)) {
1016-
ngx_qjs_exception(engine, &exception);
1017-
1018-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0, "js eval exception: %V",
1019-
&exception);
995+
ngx_qjs_log_exception(engine, ctx->log, "eval exception");
1020996
goto destroy;
1021997
}
1022998

@@ -1040,7 +1016,6 @@ ngx_engine_qjs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
10401016
{
10411017
int rc;
10421018
JSValue fn, val;
1043-
ngx_str_t exception;
10441019
JSRuntime *rt;
10451020
JSContext *cx, *cx1;
10461021

@@ -1058,10 +1033,7 @@ ngx_engine_qjs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
10581033
val = JS_Call(cx, fn, JS_UNDEFINED, nargs, &ngx_qjs_arg(args[0]));
10591034
JS_FreeValue(cx, fn);
10601035
if (JS_IsException(val)) {
1061-
ngx_qjs_exception(ctx->engine, &exception);
1062-
1063-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1064-
"js call exception: %V", &exception);
1036+
ngx_qjs_log_exception(ctx->engine, ctx->log, "call exception");
10651037

10661038
return NGX_ERROR;
10671039
}
@@ -1075,10 +1047,7 @@ ngx_engine_qjs_call(ngx_js_ctx_t *ctx, ngx_str_t *fname,
10751047
rc = JS_ExecutePendingJob(rt, &cx1);
10761048
if (rc <= 0) {
10771049
if (rc == -1) {
1078-
ngx_qjs_exception(ctx->engine, &exception);
1079-
1080-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1081-
"js job exception: %V", &exception);
1050+
ngx_qjs_log_exception(ctx->engine, ctx->log, "job exception");
10821051

10831052
return NGX_ERROR;
10841053
}
@@ -1139,7 +1108,6 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
11391108
ngx_js_loc_conf_t *conf)
11401109
{
11411110
uint32_t i, length;
1142-
ngx_str_t exception;
11431111
JSRuntime *rt;
11441112
JSValue ret;
11451113
JSContext *cx;
@@ -1156,9 +1124,7 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
11561124
if (ctx != NULL) {
11571125
ret = qjs_call_exit_hook(cx);
11581126
if (JS_IsException(ret)) {
1159-
ngx_qjs_exception(e, &exception);
1160-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1161-
"js exit hook exception: %V", &exception);
1127+
ngx_qjs_log_exception(e, ctx->log, "exit hook exception");
11621128
}
11631129

11641130
node = njs_rbtree_min(&ctx->waiting_events);
@@ -1175,9 +1141,7 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
11751141
}
11761142

11771143
if (ngx_qjs_unhandled_rejection(ctx)) {
1178-
ngx_qjs_exception(e, &exception);
1179-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1180-
"js unhandled rejection: %V", &exception);
1144+
ngx_qjs_log_exception(e, ctx->log, "unhandled rejection");
11811145
}
11821146

11831147
JS_SetHostPromiseRejectionTracker(JS_GetRuntime(cx), NULL, NULL);
@@ -1403,7 +1367,6 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14031367
{
14041368
int rc;
14051369
JSValue ret;
1406-
ngx_str_t exception;
14071370
JSRuntime *rt;
14081371
JSContext *cx1;
14091372
ngx_js_ctx_t *ctx;
@@ -1412,10 +1375,7 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14121375

14131376
ret = JS_Call(cx, fn, JS_UNDEFINED, argc, argv);
14141377
if (JS_IsException(ret)) {
1415-
ngx_qjs_exception(ctx->engine, &exception);
1416-
1417-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1418-
"js call exception: %V", &exception);
1378+
ngx_qjs_log_exception(ctx->engine, ctx->log, "call exception");
14191379

14201380
return NGX_ERROR;
14211381
}
@@ -1428,10 +1388,7 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14281388
rc = JS_ExecutePendingJob(rt, &cx1);
14291389
if (rc <= 0) {
14301390
if (rc == -1) {
1431-
ngx_qjs_exception(ctx->engine, &exception);
1432-
1433-
ngx_log_error(NGX_LOG_ERR, ctx->log, 0,
1434-
"js job exception: %V", &exception);
1391+
ngx_qjs_log_exception(ctx->engine, ctx->log, "job exception");
14351392

14361393
return NGX_ERROR;
14371394
}
@@ -1445,15 +1402,20 @@ ngx_qjs_call(JSContext *cx, JSValue fn, JSValue *argv, int argc)
14451402

14461403

14471404
ngx_int_t
1448-
ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s)
1405+
ngx_qjs_log_exception(ngx_engine_t *e, ngx_log_t *log, char *txt)
14491406
{
1450-
JSValue exception;
1407+
ngx_str_t s;
1408+
JSValue exception;
14511409

14521410
exception = JS_GetException(e->u.qjs.ctx);
1453-
if (ngx_qjs_dump_obj(e, exception, s) != NGX_OK) {
1411+
if (ngx_qjs_dump_obj(e, exception, &s) != NGX_OK) {
1412+
ngx_log_error(NGX_LOG_ERR, log, 0, "js can't get %s", txt);
1413+
14541414
return NGX_ERROR;
14551415
}
14561416

1417+
ngx_log_error(NGX_LOG_ERR, log, 0, "js %s: %V", txt, &s);
1418+
14571419
JS_FreeValue(e->u.qjs.ctx, exception);
14581420

14591421
return NGX_OK;
@@ -2240,17 +2202,15 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22402202
njs_uint_t nargs)
22412203
{
22422204
njs_int_t ret;
2243-
ngx_str_t exception;
22442205
ngx_connection_t *c;
22452206

22462207
ret = njs_vm_call(vm, func, njs_value_arg(args), nargs);
22472208
if (ret == NJS_ERROR) {
2248-
ngx_js_exception(vm, &exception);
22492209

22502210
c = ngx_external_connection(vm, njs_vm_external_ptr(vm));
22512211

2252-
ngx_log_error(NGX_LOG_ERR, c->log, 0,
2253-
"js exception: %V", &exception);
2212+
ngx_js_log_exception(vm, c->log, "exception");
2213+
22542214
return NGX_ERROR;
22552215
}
22562216

@@ -2260,10 +2220,8 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22602220
c = ngx_external_connection(vm, njs_vm_external_ptr(vm));
22612221

22622222
if (ret == NJS_ERROR) {
2263-
ngx_js_exception(vm, &exception);
2223+
ngx_js_log_exception(vm, c->log, "job exception");
22642224

2265-
ngx_log_error(NGX_LOG_ERR, c->log, 0,
2266-
"js job exception: %V", &exception);
22672225
return NGX_ERROR;
22682226
}
22692227

@@ -2276,18 +2234,22 @@ ngx_js_call(njs_vm_t *vm, njs_function_t *func, njs_opaque_value_t *args,
22762234

22772235

22782236
ngx_int_t
2279-
ngx_js_exception(njs_vm_t *vm, ngx_str_t *s)
2237+
ngx_js_log_exception(njs_vm_t *vm, ngx_log_t *log, char *txt)
22802238
{
22812239
njs_int_t ret;
2240+
ngx_str_t s;
22822241
njs_str_t str;
22832242

22842243
ret = njs_vm_exception_string(vm, &str);
22852244
if (ret != NJS_OK) {
2245+
ngx_log_error(NGX_LOG_ERR, log, 0, "js can't get %s", txt);
22862246
return NGX_ERROR;
22872247
}
22882248

2289-
s->data = str.start;
2290-
s->len = str.length;
2249+
s.data = str.start;
2250+
s.len = str.length;
2251+
2252+
ngx_log_error(NGX_LOG_ERR, log, 0, "js %s: %V", txt, &s);
22912253

22922254
return NGX_OK;
22932255
}

nginx/ngx_js.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ void ngx_js_ctx_init(ngx_js_ctx_t *ctx, ngx_log_t *log);
327327
void ngx_js_ctx_destroy(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *conf);
328328
ngx_int_t ngx_js_call(njs_vm_t *vm, njs_function_t *func,
329329
njs_opaque_value_t *args, njs_uint_t nargs);
330-
ngx_int_t ngx_js_exception(njs_vm_t *vm, ngx_str_t *s);
330+
ngx_int_t ngx_js_log_exception(njs_vm_t *vm, ngx_log_t *log, char *txt);
331331
ngx_engine_t *ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf,
332332
void *external);
333333

@@ -362,7 +362,7 @@ void ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
362362
ngx_js_loc_conf_t *conf);
363363
ngx_int_t ngx_qjs_call(JSContext *cx, JSValue function, JSValue *argv,
364364
int argc);
365-
ngx_int_t ngx_qjs_exception(ngx_engine_t *e, ngx_str_t *s);
365+
ngx_int_t ngx_qjs_log_exception(ngx_engine_t *e, ngx_log_t *log, char *txt);
366366
ngx_int_t ngx_qjs_integer(JSContext *cx, JSValueConst val, ngx_int_t *n);
367367
ngx_int_t ngx_qjs_string(JSContext *cx, ngx_pool_t *pool, JSValueConst val,
368368
ngx_str_t *dst);

nginx/ngx_stream_js_module.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,6 @@ ngx_stream_js_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
12651265
u_char *p;
12661266
njs_vm_t *vm;
12671267
njs_int_t ret;
1268-
ngx_str_t exception;
12691268
ngx_buf_t *b;
12701269
uintptr_t flags;
12711270
ngx_connection_t *c;
@@ -1310,10 +1309,7 @@ ngx_stream_js_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
13101309

13111310
if (ret == NJS_ERROR) {
13121311
error:
1313-
ngx_js_exception(vm, &exception);
1314-
1315-
ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
1316-
&exception);
1312+
ngx_js_log_exception(vm, c->log, "exception");
13171313

13181314
return NGX_ERROR;
13191315
}
@@ -2713,7 +2709,6 @@ ngx_stream_qjs_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
27132709
size_t len;
27142710
JSContext *cx;
27152711
ngx_int_t rc;
2716-
ngx_str_t exception;
27172712
ngx_buf_t *b;
27182713
uintptr_t flags;
27192714
ngx_connection_t *c;
@@ -2751,10 +2746,7 @@ ngx_stream_qjs_run_event(ngx_stream_session_t *s, ngx_stream_js_ctx_t *ctx,
27512746

27522747
if (rc == NGX_ERROR) {
27532748
error:
2754-
ngx_qjs_exception(ctx->engine, &exception);
2755-
2756-
ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
2757-
&exception);
2749+
ngx_qjs_log_exception(ctx->engine, c->log, "exception");
27582750

27592751
return NGX_ERROR;
27602752
}

0 commit comments

Comments
 (0)