0

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?

Mathias Brodala
  • 5,905
  • 13
  • 30
daniel
  • 273
  • 2
  • 7

2 Answers2

0

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

Lina
  • 1
  • sorry, it didn't work. i tried something like this: try { $news = $this->newsRepository->findByUid(50000); } catch(\Exception $e) { error_log("yay"); } – daniel Jan 24 '22 at 16:48
0

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

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34