Change default_exception_handler

Change default_exception_handler

by Colin Cruikshank -
Number of replies: 5

Hi,

I would like to redirect all uncaught exceptions to a custom error page. I've found where the site sets the default_exception_handler (lib/setup.php line 564). Is there a way to override this without changing the core code?

Thanks,

C

Average of ratings: -
In reply to Colin Cruikshank

Re: Change default_exception_handler

by Colin Cruikshank -
Follow up to this:

In an attempt to override the default exception handler, in my theme_page_initegg function (called on every page) I added:

//Override the default exception handler, route exception to oops page.
function custom_exception_handler($ex) {
redirect(new moodle_url('/local/oops.php'));
}
$OldExHdl=set_exception_handler('custom_exception_handler');

And it still doesn't work. Something is setting the exception handler back to 'default_exception_handler', but as far as I can tell the only place that is done is in setup.php, and I did a quick check using echo output, and confirmed that theme_page_initegg is called after setup.php.

Help???
In reply to Colin Cruikshank

Re: Change default_exception_handler

by Colin Cruikshank -
Another update - I have figured out why the exception handler is being reset.

After it is set to the custom_exception_handler in the theme page init, there are multiple calls to lib/ajax/service.php which in turn require config.php, which in turn requires setup.php again, which overwrites the custom_exception_handler with the default_exception_handler.

So my question remains - does anyone know a way around this without touching the moodle core code? So far everything I have tried ( a hidden block, theme page init, etc) all execute before those calls from lib/ajax/service.php.

Thank,
In reply to Colin Cruikshank

Re: Change default_exception_handler

by Colin Cruikshank -
Final reply -

I've thought of a "solution". I created a file /local/error.php which defines the function for the custom exception handler, and sets it, and then put "require_once(__DIR__ . '/local/error.php'); " directly after "require_once(__DIR__ . '/lib/setup.php');" in config.php. That way, it will always supersede the default_exception_handler set in setup.php.

If anyone has any thoughts regarding this method, or if there is a more "correct" method to override this without touching core code I am all ears. At the very least, I hope this is helpful for others down the line.

C
In reply to Colin Cruikshank

Re: Change default_exception_handler

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Presumably you don't actually want to change how the exceptions are handled.

I assume you just want to change how the exceptions are displayed.

You can do that using https://docs.moodle.org/dev/Overriding_a_renderer. Exceptions are rendered by a method in core_renderer.
In reply to Tim Hunt

Re: Change default_exception_handler

by Colin Cruikshank -
Thanks tim. This is exactly what I was looking for. I found the renderer method fatal_erroregg and overrode it via our theme. Looks like a million bucks.
Average of ratings: Useful (1)