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