[CLEANUP] Error handling classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / AbstractExceptionHandler.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * An abstract exception handler
30 *
31 * This file is a backport from FLOW3
32 *
33 * @author Ingo Renner <ingo@typo3.org>
34 */
35 abstract class AbstractExceptionHandler implements ExceptionHandlerInterface, \TYPO3\CMS\Core\SingletonInterface {
36
37 const CONTEXT_WEB = 'WEB';
38 const CONTEXT_CLI = 'CLI';
39
40 /**
41 * Displays the given exception
42 *
43 * @param \Exception $exception The exception object
44 * @return void
45 */
46 public function handleException(\Exception $exception) {
47 switch (PHP_SAPI) {
48 case 'cli':
49 $this->echoExceptionCLI($exception);
50 break;
51 default:
52 $this->echoExceptionWeb($exception);
53 }
54 }
55
56 /**
57 * Writes exception to different logs
58 *
59 * @param \Exception $exception The exception
60 * @param string $context The context where the exception was thrown, WEB or CLI
61 * @return void
62 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog(), \TYPO3\CMS\Core\Utility\GeneralUtility::devLog()
63 */
64 protected function writeLogEntries(\Exception $exception, $context) {
65 $filePathAndName = $exception->getFile();
66 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
67 $logTitle = 'Core: Exception handler (' . $context . ')';
68 $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | '
69 . get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
70 if ($context === 'WEB') {
71 $logMessage .= '. Requested URL: ' . GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
72 }
73 $backtrace = $exception->getTrace();
74 // Write error message to the configured syslogs
75 GeneralUtility::sysLog($logMessage, $logTitle, GeneralUtility::SYSLOG_SEVERITY_FATAL);
76 // When database credentials are wrong, the exception is probably
77 // caused by this. Therefor we cannot do any database operation,
78 // otherwise this will lead into recurring exceptions.
79 try {
80 // Write error message to devlog
81 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
82 if (TYPO3_EXCEPTION_DLOG) {
83 GeneralUtility::devLog($logMessage, $logTitle, 3, array(
84 'TYPO3_MODE' => TYPO3_MODE,
85 'backtrace' => $backtrace
86 ));
87 }
88 // Write error message to sys_log table
89 $this->writeLog($logTitle . ': ' . $logMessage);
90 } catch (\Exception $exception) {
91
92 }
93 }
94
95 /**
96 * Writes an exception in the sys_log table
97 *
98 * @param string $logMessage Default text that follows the message.
99 * @return void
100 */
101 protected function writeLog($logMessage) {
102 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
103 $userId = 0;
104 $workspace = 0;
105 if (is_object($GLOBALS['BE_USER'])) {
106 if (isset($GLOBALS['BE_USER']->user['uid'])) {
107 $userId = $GLOBALS['BE_USER']->user['uid'];
108 }
109 if (isset($GLOBALS['BE_USER']->workspace)) {
110 $workspace = $GLOBALS['BE_USER']->workspace;
111 }
112 }
113 $fields_values = array(
114 'userid' => $userId,
115 'type' => 5,
116 'action' => 0,
117 'error' => 2,
118 'details_nr' => 0,
119 'details' => str_replace('%', '%%', $logMessage),
120 'IP' => GeneralUtility::getIndpEnv('REMOTE_ADDR'),
121 'tstamp' => $GLOBALS['EXEC_TIME'],
122 'workspace' => $workspace
123 );
124 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
125 }
126 }
127
128 /**
129 * Sends the HTTP Status 500 code, if $exception is *not* a
130 * TYPO3\CMS\Core\Error\Http\StatusException and headers are not sent, yet.
131 *
132 * @param \Exception $exception
133 * @return void
134 */
135 protected function sendStatusHeaders(\Exception $exception) {
136 if (method_exists($exception, 'getStatusHeaders')) {
137 $headers = $exception->getStatusHeaders();
138 } else {
139 $headers = array(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500);
140 }
141 if (!headers_sent()) {
142 foreach ($headers as $header) {
143 header($header);
144 }
145 }
146 }
147
148 }