extbase controller handle exception

raw

AbstractExceptionHandlingController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Mogic\Extname\Controller;
 
use TYPO3\CMS\Core\Error\Http\PageNotFoundException;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
 
/**
 * Controller base class that catches exceptions and displays them nicely
 *
 * @author Christian Weiske <weiske@mogic.com>
 */
abstract class AbstractExceptionHandlingController extends ActionController
{
    /**
     * Message to show when an element is not found (404)
     * @var string
     */
    protected $notFoundMessage = 'Element not found';
 
    /**
     * Message to show when an unknown error occurs (e.g. API exception)
     * @var string
     */
    protected $genericErrorMessage = 'Es ist ein schwerer Fehler aufgetreten.';
 
 
    /**
     * We currently do not need this.
     *
     * Handles a request.
     * The result output is returned by altering the given response.
     *
     * @param object $request  The request object
     * @param object $response The response, modified by this handler
     *
     * @return void
     */
    public function DEACTprocessRequest(
        \TYPO3\CMS\Extbase\Mvc\RequestInterface $request,
        \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response
    ) {
        try {
            parent::processRequest($request, $response);
        } catch (PageNotFoundException $e) {
            $this->handleNotFoundError($e);
        } catch (\Exception $e) {
            $this->handleGenericError($e);
        }
    }
 
    /**
     * Call the correct action method
     *
     * @return void
     */
    public function callActionMethod()
    {
        try {
            parent::callActionMethod();
        } catch (PageNotFoundException $e) {
            $this->handleNotFoundError($e);
        } catch (\Exception $e) {
            $this->handleGenericError($e);
        }
    }
 
    /**
     * Handle a "element not found"-exception
     *
     * @param object $exception Exception
     *
     * @return void
     */
    public function handleNotFoundError(\Exception $e)
    {
        //$GLOBALS['TSFE']->pageNotFoundAndExit($this->notFoundMessage);
        $this->renderError(
            'typo3conf/ext/extname/Resources/Private/Templates/Error/NotFound.html',
            $this->notFoundMessage,
            $e
        );
        $this->response->setStatus(404);
    }
 
    /**
     * Handle a generic undetermined error
     *
     * @param object $exception Exception
     *
     * @return void
     */
    public function handleGenericError(\Exception $e)
    {
        /*
        $GLOBALS['TSFE']->pageUnavailableAndExit(
            $this->genericErrorMessage,
            'HTTP/1.1 500 Internal Server Error'
        );
        /**/
        $this->renderError(
            'typo3conf/ext/extname/Resources/Private/Templates/Error/Generic.html',
            $this->genericErrorMessage,
            $e
        );
        $this->response->setStatus(500);
    }
 
    /**
     * Render an error message into a template
     *
     * @param string $templateFile Template path
     * @param string $message      Error message
     * @param object $e            Exception that caused this error message
     *
     * @return void
     */
    protected function renderError($templateFile, $message, \Exception $e)
    {
        $this->view->setTemplatePathAndFilename($templateFile);
        $this->view->assign('error', $message);
        $this->view->assign('exception', $e);
        $this->response->setContent($this->view->render());
    }
}
?>
 
Christian Weiske Christian Weiske
owner

History