CodeIgniter Forums
Exception Handler Event - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Exception Handler Event (/showthread.php?tid=82426)



Exception Handler Event - matjlars - 07-08-2022

Hi!
I recently got a service called Honeybadger which lets me log exceptions in a really nice format. They don't have a CodeIgniter integration so I'm looking to integrate it myself. I got stuck while looking into how CodeIgniter 4 handles exceptions. I found the "exceptionHandler" function in vendor/codeigniter4/framework/system/Debug/Exception.php which is really close to what I need.  That method's comment states that it "fires an event that allows custom actions to be taken at this point."  While that sounds like exactly what I need, I can't seem to figure out what it means by "fire an event." All I want to do is call a [docs.honeybadger.io/lib/php/integration/other/] Honeybadger method, passing in any exception that happens.
What is this event thing, and how can I hook into that?
Thank you in advance.


RE: Exception Handler Event - kenjis - 07-08-2022

It seems event means CodeIgniter's Event.
https://codeigniter4.github.io/CodeIgniter4/extending/events.html#event-points

But there is no Event Points for it, and there is no code to fire an event in the exceptionHandler() method.

You can extend the Exception class.
See https://codeigniter4.github.io/CodeIgniter4/extending/core_classes.html


RE: Exception Handler Event - MGatner - 07-14-2022

@matjlars I haven’t used HoneyBadger but I use Sentry.io which does the same thing. Their SDK actually registers the listener as an exception handler, so as long as it is loaded you don’t need to do the handling yourself. Check if HoneyBadger has a similar feature. I put my registration in **app/Config/Boot/production.php** to load it early, and because I only want monitoring in production- but you could also load it in Events or Common. 
Here’s what my code looks like: 
PHP Code:
/*
 *---------------------------------------------------------------
 * SENTRY.IO
 *---------------------------------------------------------------
 * Initialize Sentry for exception reporting.
 */
if (getenv('sentry.dsn')) {
    init([
        'dsn'          => getenv('sentry.dsn'),
        'integrations' => [
            new IgnoreErrorsIntegration([
                'ignore_exceptions' => ['CodeIgniter\Exceptions\PageNotFoundException'],
            ]),
        ],
    ]);