diff --git a/config-dist.php b/config-dist.php index 31357ef6968..03dc6517953 100644 --- a/config-dist.php +++ b/config-dist.php @@ -790,6 +790,11 @@ $CFG->admin = 'admin'; // To further control this, the debug_developer_use_pretty_exceptions setting can be set to false. // $CFG->debug_developer_use_pretty_exceptions = true; // +// In many development situations it is desirable to have debugging() calls treated as errors rather than +// as exceptions. +// If this property is not specified then it will be true if pretty exceptions are usable. +// $CFG->debug_developer_debugging_as_error = true; +// // The Whoops! UI can also provide a link to open files in your preferred editor. // You can set your preferred editor by setting: // $CFG->debug_developer_editor = 'vscode'; diff --git a/lib/setuplib.php b/lib/setuplib.php index b56ba5f97a5..e95897e0723 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -387,10 +387,21 @@ function get_whoops(): ?\Whoops\Run { $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. - $info = get_exception_info($exception); - if ($info->moreinfourl) { - $collection = $inspector->getFrames(); - $collection[0]->addComment("{$info->moreinfourl}", 'More info'); + $collection = $inspector->getFrames(); + + $isdebugging = str_ends_with($collection[1]->getFile(), '/lib/weblib.php'); + $isdebugging = $isdebugging && $collection[2]->getFunction() === 'debugging'; + + if ($isdebugging) { + $remove = array_slice($collection->getArray(), 0, 2); + $collection->filter(function ($frame) use ($remove): bool { + return array_search($frame, $remove) === false; + }); + } else { + $info = get_exception_info($exception); + if ($info->moreinfourl) { + $collection[0]->addComment("{$info->moreinfourl}", 'More info'); + } } }); diff --git a/lib/weblib.php b/lib/weblib.php index a4e6dbb9cd5..c7ec6e278aa 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3320,17 +3320,26 @@ function debugging($message = '', $level = DEBUG_NORMAL, $backtrace = null) { // Script does not want any errors or debugging in output, // we send the info to error log instead. error_log('Debugging: ' . $message . ' in '. PHP_EOL . $from); - } else if ($forcedebug or $CFG->debugdisplay) { if (!defined('DEBUGGING_PRINTED')) { define('DEBUGGING_PRINTED', 1); // Indicates we have printed something. } + if (CLI_SCRIPT) { echo "++ $message ++\n$from"; } else { - echo '
' , $message , $from , '
'; - } + if (property_exists($CFG, 'debug_developer_debugging_as_error')) { + $showaserror = $CFG->debug_developer_debugging_as_error; + } else { + $showaserror = (bool) get_whoops(); + } + if ($showaserror) { + trigger_error($message, E_USER_NOTICE); + } else { + echo '
', $message, $from, '
'; + } + } } else { trigger_error($message . $from, E_USER_NOTICE); }