0

I have a extension and when I call this extension with an ID of a record that not or no longer exists I get an Exception Object of type My\Model\... with identity "1035" not found.

The page itself got the header status 500.

What I would like to do in this case is to show my default 404 page. Is this possible? And how can I do that?

2 Answers 2

1

I guess you are using a method like

public function detailAction(\YourVendor\Ext\Domain\Model\MyModel $model)

In that case, it is not that easy because the exception is already thrown in extbase's core. You can take a look how I solve it for my news extension:

   /**
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @throws \Exception
     */
    public function processRequest(RequestInterface $request, ResponseInterface $response)
    {
        try {
            parent::processRequest($request, $response);
        } catch (\Exception $exception) {
            $this->handleKnownExceptionsElseThrowAgain($exception);
        }
    }
    /**
     * @param \Exception $exception
     * @throws \Exception
     */
    private function handleKnownExceptionsElseThrowAgain(\Exception $exception)
    {
        $previousException = $exception->getPrevious();
        if (
            $this->actionMethodName === 'detailAction'
            && $previousException instanceof \TYPO3\CMS\Extbase\Property\Exception
            && isset($this->settings['detail']['errorHandling'])
        ) {
            $this->handleNoNewsFoundError($this->settings['detail']['errorHandling']);
        } else {
            throw $exception;
        }
    }

In the method handleNoNewsFoundError you can do then anything you want to, e.g. calling $GLOBALS['TSFE']->pageNotFoundAndExit('No news entry found.');.

Sign up to request clarification or add additional context in comments.

1 Comment

exactly I use the default showAction created with the extension-builder. You solutions works perfectly. For others: You have to declare the namespace or use the full path in the processRequest functionvariables.
0

Untested, but from a logical POV you should just catch the exception and then let your controller decide what to do (in your case: show a 404 page).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.