'How to catch exception: findByUid() must be an instance of News, null returned
I use TYPO3 version 10.4.23 with news extension 9.1.1
I inject the NewsRepository in my Extension to get news-title with following (simplified) code:
class MytestController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
protected $newsRepository;
public function injectNewsRepository(\GeorgRinger\News\Domain\Repository\NewsRepository $newsRepository) {
$this->newsRepository = $newsRepository;
}
public function listAction() {
$newsUid = 1234;
$news = $this->newsRepository->findByUid($newsUid);
$title = $news->getTitle();
}
}
This works fine when $newsUid is an existing ID. But if a $newsUid doesn't exist in Database, an exception is thrown:
Exception handler (WEB): Uncaught TYPO3 Exception: Return value of GeorgRinger\News\Domain\Repository\NewsRepository::findByUid() must be an instance of GeorgRinger\News\Domain\Model\News, null returned | TypeError thrown in file /www/typo3conf/ext/news/Classes/Domain/Repository/NewsRepository.php in line 355.
Unfortunately I can't catch this exception because it happens in the news extension.
What possibility do I have to catch this error in my extension?
Solution 1:[1]
just use a try-catch block then you can catch the extension. The rest of your code doesn't make sense in this case anyway. https://www.php.net/manual/en/language.exceptions.php
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lina |
