Slim仍处理错误,即使设置为不

Here is my main Slim app instantiation code:

$slimSettings = [
    'settings' => [
        'displayErrorDetails' => false,
        'routerCacheFile'     => false  # Settings::ABSPATH . '/tmp/route.cache'
    ],
];

# Initialize
$app = new \Slim\App( $slimSettings );

# Create container
$container = $app->getContainer();

# Register component on container
$container[ 'view' ] = function( $container ) {
    $renderer = new \Slim\Views\PhpRenderer();
    $renderer->setAttributes( [ 'absPath' => Settings::ABSPATH ] ); # Variable accessible from all templates

    return $renderer;
};

# Remove error handling from slim, so custom error handling is used.
unset( $app->getContainer()[ 'errorHandler' ] );

You can see that the last line is supposed to unset the errorHandler. Yet, in a controller for example I declare a function with:

public function index( $request, Response $response )

I forgot to set the 'Response' use statement, so it created an error (easily fixed but that's not the point). But this error was handled by slim. Why is that ? Isn't it supposed not to handle errors from the unset statement ?

UPDATE: error:

Details

Slim Application Error
The application could not run because of the following error:

Type: TypeError
Message: Argument 2 passed to Rib\Src\Controllers\HomeController::index() must be an instance of Rib\Src\Controllers\Response, instance of Slim\Http\Response given
File: /Users/ME/Documents/Apps_And_Sites/PHP_Apps/ribklein/src/Controllers/HomeController.php
Line: 19
duanlei7101
duanlei7101 不。如果我没有用use语句指定它,它似乎试图猜测函数参数中的'Response'应该是什么。当我这样做并指定苗条的反应时,一切都很好。无论如何,如果禁用,我不明白为什么它会处理任何错误。
3 年多之前 回复
doujing5846
doujing5846 Rib\Src\Controllers\Response是否扩展了Slim\Http\Response?
3 年多之前 回复
dougu1985
dougu1985 消息更新时出错。
3 年多之前 回复
duanbarong4617
duanbarong4617 你能发布你在这里收到的错误吗?
3 年多之前 回复

2个回答




传递给Rib \ Src \ Controllers \ HomeController :: index()的参数2必须是Rib的实例 Src \ Controllers \ Response,Slim \ Http \ Response实例</ p>
</ blockquote>

这是名称空间声明错误。答案</ p>

在顶部的控制器文件( HomeController </ code>)中添加:</ p>

 使用\ Psr \ Http \ Message \ ResponseInterface作为响应; 
</ 代码> </ pre>

更新</ strong> </ p>

抱歉,我误解了这个问题。 所以,你想知道为什么错误是由Slim处理的。</ p>

这是因为错误是PHP运行时错误。 而Slim有特殊的处理程序。 在容器中,它存储在 phpErrorHandler </ code>键下。 默认情况下,它返回 500 </ code>响应代码,当然,您可以定义自己的错误处理程序。</ p>

因此,要禁用Slim处理PHP错误, unset $ container ['phpErrorHandler'] </ code>。</ p>

重要说明:</ em>此处理程序仅限PHP7 +,因为 PHP错误类是在PHP7.0中引入的。 </ p>

你 可以阅读更多关于此处的主题。</ p>
< / DIV>

展开原文

原文

Argument 2 passed to Rib\Src\Controllers\HomeController::index() must be an instance of Rib\Src\Controllers\Response, instance of Slim\Http\Response given

This is a namespace declaration error.Answer

In your controller file (HomeController) at the top add this:

use \Psr\Http\Message\ResponseInterface as Response;

UPDATE

Sorry, I misread the question. So, you want to know why the error was handled by Slim.

This is because the error is a PHP Runtime error. And Slim has special handler for that. In container it is stored under the phpErrorHandler key. By default it returns a 500 response code, and, of course, you can define your own error handler.

Therefore, to disable Slim's handling of PHP errors, unset $container['phpErrorHandler'].

Important note: This handler is PHP7+ only, since PHP Error class was introduced in PHP7.0.

You can read more about subject here.

droebnxrm31502512
droebnxrm31502512 好吧,我特意设置了phpErrorHandler,现在苗条不会照顾它。 请更新你的答案只是添加到unset特定的phpErrorHandler,我会选择它。
3 年多之前 回复
dongsha2792
dongsha2792 不,errorHandler和phpErrorHandler是不同的处理程序。
3 年多之前 回复
douh9817
douh9817 谢谢,但没有取消设置($ app-> getContainer()['errorHandler']); 应该禁用所有错误的苗条处理?
3 年多之前 回复
drf97973
drf97973 对不起,@ RobertBrax,我已经更新了答案。
3 年多之前 回复
duanqizao6492
duanqizao6492 问题不是错误。 这就是为什么苗条抓住它即使设置不。
3 年多之前 回复

From the error output you posted it looks like that Slim did not handle the error and instead the default php error handler was used.

A Slim error output would look something like this:

Slim Application Error

The application could not run because of the following error:
Details
Type: TypeError
Message: Argument 1 passed to Sleeti\Controllers\Authentication\a() must implement interface Psr\Http\Message\ServerRequestInterface, array given, called in C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php on line 62
File: C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php
Line: 58
Trace

#0 C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php(62): Sleeti\Controllers\Authentication\a(Array)
#1 [internal function]: Sleeti\Controllers\Authentication\AuthenticationController->postLogin(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Route.php(340): Slim\Handlers\Strategies\RequestResponse->__invoke(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#4 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\GuestMiddleware.php(27): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 [internal function]: Sleeti\Middleware\GuestMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#6 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\GuestMiddleware), Array)
#7 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#8 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#9 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(438): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\LocalizationMiddleware.php(29): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 [internal function]: Sleeti\Middleware\LocalizationMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#14 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\LocalizationMiddleware), Array)
#15 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#16 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#17 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\LoggerMiddleware.php(36): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#18 [internal function]: Sleeti\Middleware\LoggerMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#19 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\LoggerMiddleware), Array)
#20 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#21 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#22 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#23 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(332): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#24 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(293): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#25 C:\Source\Web\PHP\Apps\SlimPomf\public\index.php(5): Slim\App->run()
#26 {main}
duanduan1993
duanduan1993 不,它显示了你所有的印刷品。 为了获得空间我只发布了错误目标,但我确实看到了细长的错误文本和整个堆栈跟踪。 我更新了我的帖子。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问