MDL-81419 core: Ensure that frames exist before checking them

This commit is contained in:
Andrew Nicols 2024-04-03 15:38:16 +08:00
parent 4da813d35e
commit 794c62db58
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14

View File

@ -385,11 +385,13 @@ function get_whoops(): ?\Whoops\Run {
// Append a custom handler to add some more information to the frames.
$whoops->appendHandler(function ($exception, $inspector, $run) {
// Moodle exceptions often have a link to the Moodle docs pages for them.
// Add that to the first frame in the stack.
$collection = $inspector->getFrames();
$isdebugging = str_ends_with($collection[1]->getFile(), '/lib/weblib.php');
// Detect if the Whoops handler was immediately invoked by a call to `debugging()`.
// If so, we remove the top frames in the collection to avoid showing the inner
// workings of debugging, and the point that we trigger the error that is picked up by Whoops.
$isdebugging = count($collection) > 2;
$isdebugging = $isdebugging && str_ends_with($collection[1]->getFile(), '/lib/weblib.php');
$isdebugging = $isdebugging && $collection[2]->getFunction() === 'debugging';
if ($isdebugging) {
@ -398,6 +400,8 @@ function get_whoops(): ?\Whoops\Run {
return array_search($frame, $remove) === false;
});
} else {
// Moodle exceptions often have a link to the Moodle docs pages for them.
// Add that to the first frame in the stack.
$info = get_exception_info($exception);
if ($info->moreinfourl) {
$collection[0]->addComment("{$info->moreinfourl}", 'More info');