inspector-apm / inspector-codeigniter

Connect your CodeIgniter application to Inspector monitoring dashboard.
MIT License
2 stars 1 forks source link

Unhandled Exceptions are no longer sent to the inspector dashboard #6

Closed benedict-tshaba closed 11 months ago

benedict-tshaba commented 11 months ago

New in version 4.4.0. of CodeIgniter.

There has been a change in the way the framework handles exceptions, see https://codeigniter.com/user_guide/general/errors.html#using-exceptions

The issue now is that the default error/exception handler will be called instead of the libraries' recordUnhandledException method. This means that unhandled exceptions will not be recorded/reported to the inspector dashboard like they used to even if the config parameter LogUnhandledExceptions is set to true.

There is a quick work around for now, edit the handler method of app/Config/Exceptions.php to look like this:

public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
{
    \Config\Services::inspector()->reportException($exception);
    return new ExceptionHandler($this);
}

Not really sure how to handle the issue in a 'nice' way currently. But will make a pull request once we have something solid.

benedict-tshaba commented 11 months ago

Possible Solution

We have a possible solution which seems to be working very well in our testing environments. We will be deploying these changes to production on Monday, then we will monitor the results over the week. If everything works as we expect it should, we can then consider making a pull request for this.

In src/Config/Events.php image

We add a listener for the 'pre_system' event, at this point only a few components of the CodeIgniter framework have been initialised, so we are safe to add/set an exception handler. From the listener we call the initialize method of the inspector library which will setup the exception handlers.

In src/Inspector.php image

We create a function to wrap the call to recordUnhandledException, which allows us to do a few other things as well, this way we can record any previous exception handlers, such as the one added by the CodeIgniter framework. Then in the recordUnhandledException method, we can call the reportException method, to send through our exception to the inspector dashboard, and finally cycle through all the stored exception handlers and call them in sequence.

This allows any previous exception handlers to still be honoured while we continue to enjoy timely error reporting in our applications.

ilvalerione commented 11 months ago

This incopatibility should be reflected in the composer.json file.

Maybe "codeigniter4/codeigniter4": ">=4.4.0" can make the job.

benedict-tshaba commented 11 months ago

This incopatibility should be reflected in the composer.json file.

Maybe "codeigniter4/codeigniter4": ">=4.4.0" can make the job.

I don't think that is necessary, we have an Application that is running on CodeIgniter 4.3.2, and it is still working correctly with these changes. So this isn't really a breaking change.

i.e. The changes will still work for users who are on a previous version of the CodeIgniter framework.