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 if ($context === 'WEB') {
70 $logMessage .= '. Requested URL: ' . t3lib_div
::getIndpEnv('TYPO3_REQUEST_URL');
73 $backtrace = $exception->getTrace();
75 // write error message to the configured syslogs
76 t3lib_div
::sysLog($logMessage, $logTitle, 4);
78 // When database credentials are wrong, the exception is probably
79 // caused by this. Therefor we cannot do any database operation,
80 // otherwise this will lead into recurring exceptions.
82 // In case an error occurs before a database connection exists, try
83 // to connect to the DB to be able to write the devlog/sys_log entry
84 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link
)) {
85 $GLOBALS['TYPO3_DB']->connectDB();
88 // write error message to devlog
89 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
90 if (TYPO3_EXCEPTION_DLOG
) {
96 'TYPO3_MODE' => TYPO3_MODE
,
97 'backtrace' => $backtrace
102 // write error message to sys_log table
103 $this->writeLog($logTitle . ': ' . $logMessage);
104 } catch (Exception
$exception) {
105 // Nothing happens here. It seems the database credentials are wrong
110 * Writes an exception in the sys_log table
112 * @param string Default text that follows the message.
115 protected function writeLog($logMessage) {
116 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link
)) {
119 if (is_object($GLOBALS['BE_USER'])) {
120 if (isset($GLOBALS['BE_USER']->user
['uid'])) {
121 $userId = $GLOBALS['BE_USER']->user
['uid'];
123 if (isset($GLOBALS['BE_USER']->workspace
)) {
124 $workspace = $GLOBALS['BE_USER']->workspace
;
128 $fields_values = Array(
134 'details' => $logMessage,
135 'IP' => t3lib_div
::getIndpEnv('REMOTE_ADDR'),
136 'tstamp' => $GLOBALS['EXEC_TIME'],
137 'workspace' => $workspace
140 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
145 * Sends the HTTP Status 500 code, if $exception is *not* a t3lib_error_http_StatusException
146 * and headers are not sent, yet.
148 * @param Exception $exception
151 protected function sendStatusHeaders(Exception
$exception) {
152 if (method_exists($exception, 'getStatusHeaders')) {
153 $headers = $exception->getStatusHeaders();
155 $headers = array(t3lib_utility_Http
::HTTP_STATUS_500
);
157 if (!headers_sent()) {
158 foreach($headers as $header) {
165 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php'])) {
166 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']);