php - CakePHP:控制台命令中的自定义错误报告

标签 php cakephp error-handling

我们目前使用 CakePHP 2.4.7 和自定义错误处理程序。自定义错误处理程序适用于通过 HTTP 或 CronDispatcher 发出的每个请求。

不幸的是,当向我们的控制台命令之一发出控制台请求时,错误处理程序被忽略。

请参阅以下示例:

core.php:

App::uses('SentryErrorHandler', 'Sentry.Lib');

Configure::write('Sentry', array(
  //'production_only' => true, // true is default value -> no error in sentry when debug
  'PHP' => array(
    'server' => 'https://XXX:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d0404041d3c2d2d733a38292e3833292f24733e3230" rel="noreferrer noopener nofollow">[email protected]</a>/1234'
  )
));

Configure::write('Error', array(
  'handler' => 'SentryErrorHandler::handleError',
  'level' => E_ALL & ~E_DEPRECATED,
  'trace' => true
));

Configure::write('Exception', array(
  'handler' => 'SentryErrorHandler::handleException',
  'renderer' => 'ExceptionRenderer',
  'log' => true
));

XYController.php/XYShell.php:

function test() {
  die(pr([]['test']));
}

在这两种情况下,都会引发正确的错误:

PHP Parse error:  syntax error, unexpected '[' in /vagrant/htdocs/app/Console/Command/XYShell.php on line 50

Parse error: syntax error, unexpected '[' in /vagrant/htdocs/app/Console/Command/XYShell.php on line 50
Fatal Error Error: syntax error, unexpected '[' in [/vagrant/htdocs/app/Console/Command/XYShell.php, line 50]

但是,当通过 ./cake XY 测试调用上述方法时,错误似乎没有正确传播到自定义错误处理程序。

我错过了什么吗?

最佳答案

控制台和网页不使用相同的配置

异常和错误配置有 separate options标准异常和错误配置:

consoleHandler - callback - The callback used to handle errors when running in the console. If undefined, CakePHP’s default handlers will be used.

这也是显而易见的 from the source .

因此,要配置用于 Web 和 cli 请求的不同处理程序 - 定义 handlerconsoleHandler 键:

Configure::write('Error', array(
  'handler' => 'SentryErrorHandler::handleError',
  'consoleHandler' => 'SentryErrorHandler::handleError', # <--
  'level' => E_ALL & ~E_DEPRECATED,
  'trace' => true
));

Configure::write('Exception', array(
  'handler' => 'SentryErrorHandler::handleException',
  'consoleHandler' => 'SentryErrorHandler::handleException', # <--
  'renderer' => 'ExceptionRenderer',
  'log' => true
));

关于php - CakePHP:控制台命令中的自定义错误报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635430/

相关文章:

cakephp - 使用 CakePHP 模型关联从多个表中检索值

powershell - 主机未知时如何捕获 plink 失败

javascript - 对于这个 promise 安排,我可以省略这个 catch block 吗?

php - 使用 htaccess 清理 URL

php - 数据表Ajax源

wamp 上的 php mssql 扩展

asp.net - 使用格式错误的 Json 调用 ASP.NET WebMethod 时捕获错误

php - 改变多列的列类型

Cakephp 3 不在查询中

php - 无法从 ubuntu 服务器上的 cakephp shell 发送电子邮件