[CLEANUP] Error handling classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / ProductionExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A quite exception handler which catches but ignores any exception.
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Ingo Renner <ingo@typo3.org>
32 */
33 class ProductionExceptionHandler extends AbstractExceptionHandler {
34
35 /**
36 * Default title for error messages
37 *
38 * @var string
39 */
40 protected $defaultTitle = 'Oops, an error occurred!';
41
42 /**
43 * Default message for error messages
44 *
45 * @var string
46 */
47 protected $defaultMessage = '';
48
49 /**
50 * Constructs this exception handler - registers itself as the default exception handler.
51 *
52 * @author Robert Lemke <robert@typo3.org>
53 */
54 public function __construct() {
55 set_exception_handler(array($this, 'handleException'));
56 }
57
58 /**
59 * Echoes an exception for the web.
60 *
61 * @param \Exception $exception The exception
62 * @return void
63 */
64 public function echoExceptionWeb(\Exception $exception) {
65 $this->sendStatusHeaders($exception);
66 $this->writeLogEntries($exception, self::CONTEXT_WEB);
67 $messageObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
68 'TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage',
69 $this->getMessage($exception),
70 $this->getTitle($exception)
71 );
72 $messageObj->output();
73 }
74
75 /**
76 * Echoes an exception for the command line.
77 *
78 * @param \Exception $exception The exception
79 * @return void
80 */
81 public function echoExceptionCLI(\Exception $exception) {
82 $this->writeLogEntries($exception, self::CONTEXT_CLI);
83 die(1);
84 }
85
86 /**
87 * Determines, whether Exception details should be outputted
88 *
89 * @param \Exception $exception The exception
90 * @return boolean
91 */
92 protected function discloseExceptionInformation(\Exception $exception) {
93 // Show client error messages 40x in every case
94 if ($exception instanceof Http\AbstractClientErrorException) {
95 return TRUE;
96 }
97 // Only show errors in FE, if a BE user is authenticated
98 if (TYPO3_MODE === 'FE') {
99 return $GLOBALS['TSFE']->beUserLogin;
100 }
101 return TRUE;
102 }
103
104 /**
105 * Returns the title for the error message
106 *
107 * @param \Exception $exception Exception causing the error
108 * @return string
109 */
110 protected function getTitle(\Exception $exception) {
111 if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
112 return htmlspecialchars($exception->getTitle());
113 } else {
114 return $this->defaultTitle;
115 }
116 }
117
118 /**
119 * Returns the message for the error message
120 *
121 * @param \Exception $exception Exception causing the error
122 * @return string
123 */
124 protected function getMessage(\Exception $exception) {
125 if ($this->discloseExceptionInformation($exception)) {
126 // Exception has an error code given
127 if ($exception->getCode() > 0) {
128 $moreInformationLink = '<p>More information regarding this error might be available <a href="'
129 . TYPO3_URL_EXCEPTION . $exception->getCode() . '" target="_blank">online</a>.</p>';
130 } else {
131 $moreInformationLink = '';
132 }
133 return htmlspecialchars($exception->getMessage()) . $moreInformationLink;
134 } else {
135 return $this->defaultMessage;
136 }
137 }
138
139 }