Skip to content

Commit 86108b5

Browse files
committed
feat(agent): report exceptions caught by CakePHP error handler
1 parent c6f59c8 commit 86108b5

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

agent/fw_cakephp.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -269,30 +269,38 @@ NR_PHP_WRAPPER_END
269269
/*
270270
* CakePHP 4.0+
271271
*
272-
* If the action or controller is not found during the dispatch process, the
273-
* appropriate Exception will be created and thrown. We wrap the CakeException
274-
* constructor instead of the Exception handler, since CakePHP allows for the
275-
* handler to be completely replaced.
276-
*
277-
* txn naming scheme:
278-
* In this case, `nr_txn_set_path` is called before `NR_PHP_WRAPPER_CALL` with
279-
* `NR_NOT_OK_TO_OVERWRITE` and as this corresponds to calling the wrapped
280-
* function in func_begin it needs to be explicitly set as a before_callback to
281-
* ensure OAPI compatibility. This entails that the first wrapped call gets to
282-
* name the txn.
272+
* Report errors and exceptions caught by CakePHP's error handler.
283273
*/
284-
NR_PHP_WRAPPER(nr_cakephp_problem_4) {
285-
const char* name = "Exception";
274+
NR_PHP_WRAPPER(nr_cakephp_error_handler_wrapper) {
275+
zval* exception = NULL;
276+
char* request_uri
277+
= nr_strdup(nr_php_get_server_global("REQUEST_URI" TSRMLS_CC));
286278

287-
(void)wraprec;
288279
NR_UNUSED_SPECIALFN;
280+
(void)wraprec;
289281

290282
NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_CAKEPHP);
291283

292-
nr_txn_set_path("CakePHP", NRPRG(txn), name, NR_PATH_TYPE_ACTION,
284+
exception = nr_php_arg_get(1, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
285+
if (!nr_php_is_zval_valid_object(exception)) {
286+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: exception is NULL or not an object",
287+
__func__);
288+
goto end;
289+
}
290+
291+
if (NR_SUCCESS
292+
!= nr_php_error_record_exception(
293+
NRPRG(txn), exception, nr_php_error_get_priority(E_ERROR), true,
294+
"Uncaught exception ", &NRPRG(exception_filters) TSRMLS_CC)) {
295+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: unable to record exception", __func__);
296+
}
297+
298+
nr_txn_set_path("CakePHP", NRPRG(txn), request_uri, NR_PATH_TYPE_ACTION,
293299
NR_NOT_OK_TO_OVERWRITE);
294300

295-
NR_PHP_WRAPPER_CALL;
301+
end:
302+
nr_php_arg_release(&exception);
303+
nr_free(request_uri);
296304
}
297305
NR_PHP_WRAPPER_END
298306

@@ -313,16 +321,10 @@ void nr_cakephp_enable(TSRMLS_D) {
313321
nr_php_wrap_user_function(
314322
NR_PSTR("Cake\\Controller\\Controller::invokeAction"),
315323
nr_cakephp_name_the_wt_4 TSRMLS_CC);
324+
nr_php_wrap_user_function(
325+
NR_PSTR(
326+
"Cake\\Error\\Middleware\\ErrorHandlerMiddleware::handleException"),
327+
nr_cakephp_error_handler_wrapper TSRMLS_CC);
316328
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
317329
PHP_PACKAGE_VERSION_UNKNOWN);
318-
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
319-
&& !defined OVERWRITE_ZEND_EXECUTE_DATA
320-
nr_php_wrap_user_function_before_after_clean(
321-
NR_PSTR("Cake\\Core\\Exception\\CakeException::__construct"),
322-
nr_cakephp_problem_4, NULL, NULL);
323-
#else
324-
nr_php_wrap_user_function(
325-
NR_PSTR("Cake\\Core\\Exception\\CakeException::__construct"),
326-
nr_cakephp_problem_4 TSRMLS_CC);
327-
#endif
328330
}

0 commit comments

Comments
 (0)