Search code examples
cakephpcakephp-2.0newrelic

cakephp 2 how to suppress or catch missing controller exception


Whenever somebody tries to access a non-existant url on my cakephp app I get a missing controller exception like this:

MissingControllerException: Uncaught exception 'MissingControllerException' with message 'Controller class AutodiscoverController could not be found.' in /app/Vendor/cakephp/cakephp/lib/Cake/Routing/Dispatcher.php:161

I guess this is as it should be, but it creates a problem with New Relic. This is our monitoring application and it will always register these exceptions and notify everybody that there is a problem with the application.

Is there a way to catch the exception or suppress it so that New Relic does not register it?


Solution

  • This is how I ended up solving my problem:

    I installed new relic's PHP agent: sudo apt-get install newrelic-php5

    Then I configured an ExceptionHandler for my app. In core.php: Configure::write('Exception.handler', 'AppExceptionHandler::handleException');

    In bootstrap.php: App::uses('AppExceptionHandler', 'Lib');

    The handler is located in app/Lib/AppExceptionHandler.php and it looks like this:

    <?php
    class AppExceptionHandler extends ErrorHandler{
        public static function handleException($error) {
            if(get_class($error) == 'MissingControllerException') {
                if (extension_loaded('newrelic')) {
                    newrelic_ignore_transaction();
                }
            }
            parent::handleException($error);
        }
    }
    

    The handler filters through all exceptions and if a MissingControllerException comes it's way, it uses the New Relic PHP Agent to ignore the current transaction. After the filtering the normal handleException() method by cake's ErrorHandler is excecuted.