2 /***************************************************************
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
27 * An abstract exception handler
29 * This file is a backport from FLOW3
32 * @subpackage t3lib_error
34 abstract class t3lib_error_AbstractExceptionHandler
implements t3lib_error_ExceptionHandlerInterface
, t3lib_Singleton
{
35 const CONTEXT_WEB
= 'WEB';
36 const CONTEXT_CLI
= 'CLI';
39 * Displays the given exception
41 * @param Exception $exception The exception object
44 public function handleException(Exception
$exception) {
47 $this->echoExceptionCLI($exception);
50 $this->echoExceptionWeb($exception);
56 * Writes exception to different logs
58 * @param Exception $exception The exception
59 * @param string the context where the exception was thrown, WEB or CLI
61 * @see t3lib_div::sysLog(), t3lib_div::devLog()
63 protected function writeLogEntries(Exception
$exception, $context) {
64 $filePathAndName = $exception->getFile();
65 $exceptionCodeNumber = ($exception->getCode() > 0) ?
'#' . $exception->getCode() . ': ' : '';
66 $logTitle = 'Core: Exception handler (' . $context . ')';
67 $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | ' .
68 get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
69 $backtrace = $exception->getTrace();
71 // write error message to the configured syslogs
72 t3lib_div
::sysLog($logMessage, $logTitle, 4);
74 // When database credentials are wrong, the exception is probably
75 // caused by this. Therefor we cannot do any database operation,
76 // otherwise this will lead into recurring exceptions.
78 // In case an error occurs before a database connection exists, try
79 // to connect to the DB to be able to write the devlog/sys_log entry
80 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link
)) {
81 $GLOBALS['TYPO3_DB']->connectDB();
84 // write error message to devlog
85 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
86 if (TYPO3_EXCEPTION_DLOG
) {
92 'TYPO3_MODE' => TYPO3_MODE
,
93 'backtrace' => $backtrace
98 // write error message to sys_log table
99 $this->writeLog($logTitle . ': ' . $logMessage);
100 } catch (Exception
$exception) {
101 // Nothing happens here. It seems the database credentials are wrong
106 * Writes an exception in the sys_log table
108 * @param string Default text that follows the message.
111 protected function writeLog($logMessage) {
112 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link
)) {
115 if (is_object($GLOBALS['BE_USER'])) {
116 if (isset($GLOBALS['BE_USER']->user
['uid'])) {
117 $userId = $GLOBALS['BE_USER']->user
['uid'];
119 if (isset($GLOBALS['BE_USER']->workspace
)) {
120 $workspace = $GLOBALS['BE_USER']->workspace
;
124 $fields_values = Array(
130 'details' => $logMessage,
131 'IP' => t3lib_div
::getIndpEnv('REMOTE_ADDR'),
132 'tstamp' => $GLOBALS['EXEC_TIME'],
133 'workspace' => $workspace
136 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
141 * Sends the HTTP Status 500 code, if $exception is *not* a t3lib_error_http_StatusException
142 * and headers are not sent, yet.
144 * @param Exception $exception
147 protected function sendStatusHeaders(Exception
$exception) {
148 if (method_exists($exception, 'getStatusHeaders')) {
149 $headers = $exception->getStatusHeaders();
151 $headers = array(t3lib_utility_Http
::HTTP_STATUS_500
);
153 if (!headers_sent()) {
154 foreach($headers as $header) {
161 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php'])) {
162 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']);