【问题标题】:CakePHP: Custom Error reporting in Console CommandsCakePHP:控制台命令中的自定义错误报告
【发布时间】:2014-05-13 15:26:09
【问题描述】:

我们目前正在使用 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:YYY@app.getsentry.com/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 测试调用上述方法时,看起来错误没有正确传播到自定义错误处理程序。

我错过了什么吗?

【问题讨论】:

  • ` 或通过 CronDispatcher` 与您的问题无关,但“哦,天哪...”。不需要 cron 调度程序。
  • 我知道,我们正在替换越来越多的 cron 调度程序代码 ;)

标签: php cakephp error-handling


【解决方案1】:

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

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

consoleHandler - 回调 - 在控制台中运行时用于处理错误的回调。如果未定义,将使用 CakePHP 的默认处理程序。

这也很明显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
));

【讨论】:

  • 使用上述代码时,我在控制器初始化错误中运行。看起来,ExceptionRenderer 正在尝试发送 http 响应。在控制台环境中,我认为控制器的初始化根本没有意义。这是 SentryErrorHandler 代码的问题吗(github.com/justnom/cakephp-sentry/blob/master/Lib/… - 因为该类没有扩展 ConsoleErrorHandler)?
  • 我没用那个插件,不知道你说的是什么初始化错误。然而,它看起来像it is written to expect a web request,当然不会有任何用于 cli 请求的用户上下文,因此创建一个 ConsoleExceptionHandler extending the core one 可能是合适的 - 如果是我,我会 PR 更改 =)。跨度>
  • 我已经尝试基于核心 ConsoleHandler 公关这个更改,但我在那里发现了另一个问题:stackoverflow.com/questions/23656173/… :/
猜你喜欢
  • 2013-10-19
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
相关资源
最近更新 更多