gpt4 book ai didi

symfony - Silex-我的错误处理程序不起作用

转载 作者:行者123 更新时间:2023-12-03 07:50:02 25 4
gpt4 key购买 nike

我试图在 Controller 上设置错误处理程序,以捕获可能导致页面故障的所有内容。例如:在这种情况下,我试图捕获我的方法使用错误的参数调用外部API后可能发生的任何错误,但是除了给我提供典型的ClientException in Middleware.php line 69:
Client error: 400
而不是我自己的代码之外,它似乎没有任何作用正是为了任何建议都将不胜感激,或者可以更好地解决Silex中的错误。

private function getSIS($url, $session, Application $app)
{
$message = "You don't have permission to access this!";

if($app['security']->isGranted('ROLE_SUPER_ADMIN'))
{
$client = new Client(['base_uri' => 'https://***********.ca/api/SIS/']);
if (!empty($session))
$response = $client->get($url, ['query' => 'session=' . $session]);
else
$response = $client->get($url);
return $response;
}

$app->error(function (\Exception $e, $code) {
switch($code) {
case 404:
$message = 'The requested page could not be found';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}

return new Response($message);
});

return new Response($message);
}

最佳答案

$app->error方法可能需要放置在 Controller 操作的上下文之外。我不确定您的应用程序的结构如何,但也许尝试将错误块放在$app->run();之前

$app->error(function (\Exception $e, $code) use ($app) {
switch($code) {
case 404:
$message = 'The requested page could not be found';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}

return new Response($message, $code);
});

$app->run();

关于symfony - Silex-我的错误处理程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com