【发布时间】:2018-04-06 01:55:26
【问题描述】:
我无法让 phpErrorHandler 工作。我使用的是最新版本的 Slim,我的服务器运行的是 PHP 7.1。
use \Slim\App as App;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new App([
'errorHandler' => function ($c) {
return function ($request, $response, $error) use ($c) {
return $c['response']
->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('application error');
};
},
'phpErrorHandler' => function ($c) {
return function ($request, $response, $error) use ($c) {
return $c['response']
->withStatus(500)
->withHeader('Content-Type', 'text/html')
->write('runtime error');
};
}
]);
当我从代码中抛出异常时,将调用错误处理程序并且我的输出是“应用程序错误”。但是,当我从代码中抛出错误或引入语法错误时,不会调用 phpErrorHandler,并且根据我的设置,PHP 会生成带有堆栈跟踪的错误消息。
我已经尝试添加
ini_set('display_errors', 0); // do not produce the standard output when an error occurs
error_reporting(-1); // enable error reporting (is this required for Slim?)
但是我在该配置中所做的任何更改似乎都不会影响我的闭包是否被调用。
-编辑-
我尝试执行一段记录到控制台的 Javascript。这适用于errorHandler,不适用于phpErrorHandler。也很简单
die('oops');
在 phpErrorHandler 中不做任何事情。似乎根本没有调用回调。
如果我尝试/捕捉
$app->run();
我能够发现错误。但是,这会导致一些我更愿意避免的讨厌的副作用。不过,这告诉我的是,它不是阻止我捕捉错误的配置设置。
【问题讨论】:
-
我的系统设置是 PHP 7.1。我无法重现您的问题。抛出
Exception或Error都会触发phpErrorHandler处理程序,因为这两个类都实现了Throwable接口。 -
只是好奇,你能发布你的
phpinfo()输出吗? -
我的猜测是
$c['response']与函数接收的$response参数不同;试试这个