phptypo3tx-news

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.

https://github.com/georgringer/news/blob/76c09fca1f57d37585f0d8286c002e155c0746e5/Classes/Domain/Repository/NewsRepository.php#L355

What possibility do I have to catch this error in my extension?


Solution

  • There is nothing you could really do as the return type needs to be nullable. This is now achieved by the change https://github.com/georgringer/news/commit/ca58c4f13fc7f21cf89d2d52ad9ecd4ac625e9d8