Http 请求不会捕获所有错误

Http Request doesn't catch all errors

我目前正在 symfony 中做一个项目,我的数据库中存储了很多链接。按下一个按钮,我想用 http 请求验证所有链接。

到目前为止,我所拥有的在大多数情况下都运行良好:

我有一个发送请求和returns状态代码的服务

    public function validateUrl($query) 
    {
        $response = $this->client->request(
            'GET',
            $query
        );

        $statusCode = $response->getStatusCode();

        return $statusCode;
    }

在调用服务的验证函数中我有这个

public function validateAll(UrlValidatorService $urlValidatorService)
    {
        $request = Request::createFromGlobals();
        $referrer = $request->query->get('referrer');

        $repository = $this->getDoctrine()->getRepository(Url::class);
        $longUrls = $repository->findBy([], ['id' => 'ASC']);
        
        foreach($longUrls as $urlEntity)
        {
            $url = $urlEntity->getLongUrl();
            $statusCode = $urlValidatorService->validateUrl($url);
            $datestring = date('Y-m-d H:i:s');

            if($statusCode == 200) 
            {
                $urlEntity->setValidStatus('valid');
                $urlEntity->setValidDate(new \DateTime($datestring));
            }

            if($statusCode >= 300)
            {
                $urlEntity->setValidStatus('invalid');
                $urlEntity->setValidDate(new \DateTime($datestring));
            }

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($urlEntity);
            $entityManager->flush(); 
        }

        return $this->redirect($referrer);
    }

正如我所说,这在大多数情况下都有效,但有一些错误由于某种原因无法捕捉到,我不知道如何解决。感谢任何帮助

无法捕获的错误示例: Example Error

好的,我明白了。尽管 https://github.com/symfony/symfony/issues/34281 声明捕获错误不起作用,因为响应是惰性的,但我能够像这样捕获它们:

    public function validateUrl($query) 
    {
        try{
            $response = $this->client->request('GET', $query, ['timeout' => 5]);

            $response->getContent(false);
            $statusCode = $response->getStatusCode();
        }
        catch (HttpExceptionInterface $e) {    
            $statusCode = '404';
        }
        // Exceptions like Timeout
        catch (TransportExceptionInterface $e) {
            $statusCode = '404';
        }
        
        return $statusCode;
    }