Skip to content

Commit 930d6f4

Browse files
committed
Include stack trace when JS exceptions are thrown into PHP.
1 parent b4f1b47 commit 930d6f4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/node_php_jsobject_class.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,32 @@ static zend_object_handlers node_php_jsobject_handlers;
6262
if (msg.HasException()) { \
6363
ZVal e{ZEND_FILE_LINE_C}; \
6464
msg.exception().ToPhp(obj->channel, e TSRMLS_CC); \
65-
e.Separate(); \
66-
convert_to_string(e.Ptr()); \
67-
/* XXX attach the wrapped JS exception XX */ \
68-
zend_throw_exception_ex( \
69-
zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, \
70-
fmt ": %*s", __VA_ARGS__, Z_STRLEN_P(*e), Z_STRVAL_P(*e)); \
65+
throwWrappedException(&e TSRMLS_CC); \
7166
return; \
7267
} \
7368
} while (0)
7469

70+
static void throwWrappedException(ZVal *e TSRMLS_DC) {
71+
// Try to get a "stack" property, otherwise use the given exception.
72+
if (e->IsObject()) {
73+
zval *r = zend_read_property(Z_OBJCE_P(e->Ptr()), e->Ptr(),
74+
"stack", 5, 1 TSRMLS_CC);
75+
Z_ADDREF_P(r);
76+
if (EG(exception)) {
77+
zend_clear_exception(TSRMLS_C); // ignore
78+
} else {
79+
e->Set(r ZEND_FILE_LINE_CC);
80+
}
81+
if (r) { zval_ptr_dtor(&r); }
82+
}
83+
e->Separate();
84+
convert_to_string(e->Ptr());
85+
zend_throw_exception_ex(
86+
zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
87+
"JS: %*s", Z_STRLEN_P(e->Ptr()), Z_STRVAL_P(e->Ptr()));
88+
return;
89+
}
90+
7591
/* JsObject handlers */
7692

7793
class JsHasPropertyMsg : public MessageToJs {

0 commit comments

Comments
 (0)