cakephp 2 如何抑制或捕获丢失的控制器异常

cakephp 2 how to suppress or catch missing controller exception

每当有人试图在我的 cakephp 应用程序上访问不存在的 url 时,我都会收到一个丢失的控制器异常,如下所示:

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

我想这是应该的,但它给 New Relic 带来了问题。这是我们的监控应用程序,它会一直记录这些异常并通知每个人应用程序有问题。

有没有办法捕获或抑制异常,使 New Relic 不注册它?

是的,你可以在你的 App controller -> beforeFilter 函数中管理它

if($this->name == 'CakeError'){
// Perform any action in error
}

这就是我最终解决问题的方式:

我安装了新遗迹的 PHP 代理:sudo apt-get install newrelic-php5

然后我为我的应用程序配置了一个 ExceptionHandler。在 core.php 中: Configure::write('Exception.handler', 'AppExceptionHandler::handleException');

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

处理程序位于 app/Lib/AppExceptionHandler.php 中,如下所示:

<?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);
    }
}

处理程序过滤所有异常,如果出现 MissingControllerException,它会使用 New Relic PHP 代理忽略当前事务。过滤后cake的ErrorHandler正常的handleException()方法被执行。