3 * An include file for setting up the exception handler of the hub
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 // The node's own exception handler
25 function hub_exception_handler ($exceptionInstance) {
26 // Is it an object and a valid instance?
27 if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof FrameworkException)) {
31 // Get all call levels from backtrace
32 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
33 // Init argument string
37 if ((isset($traceArray['args'])) && (is_array($traceArray['args'])) && (count($traceArray['args']) > 0)) {
38 // Convert arguments type into human-readable
39 foreach ($traceArray['args'] as $arg) {
40 $argsString .= ', ' . gettype($arg);
42 $argsString = substr($argsString, 2);
45 // Set missing file/line
46 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
47 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
48 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
49 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
51 $backTrace .= sprintf("---------- Pos %d: ----------
59 $traceArray['function'],
61 basename($traceArray['file']),
66 // Construct the message
67 $message = sprintf("--------------------------------------------------------------------------------
68 Uncaught Exception : %s
69 --------------------------------------------------------------------------------
74 --------------------------------------------------------------------------------
76 --------------------------------------------------------------------------------
78 --------------------------------------------------------------------------------\n",
79 trim(html_entity_decode(strip_tags($exceptionInstance->__toString()))),
80 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
81 $exceptionInstance->getHexCode(),
82 $exceptionInstance->getFile(),
83 $exceptionInstance->getLine(),
91 * Invalid exception instance detected! Do *only* throw exceptions that
92 * extends our own exception 'FrameworkException' to get such nice
95 print('exceptionInstance[]=' . gettype($exceptionInstance) . ' is invalid! Please inform the core developer team.');
100 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
101 // Construct the message
102 $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
109 // Throw an exception here
110 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
114 function __assertHandler ($file, $line, $code) {
117 $code = '<em>Unknown</em>';
121 $message = sprintf('File: %s, Line: %s, Code: %s',
128 die($message . PHP_EOL);
129 syslog(LOG_WARNING, $message);
131 // Throw an exception here
132 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
136 //set_error_handler('__errorHandler');
138 // Set the exception handler
139 set_exception_handler('hub_exception_handler');
141 // Init assert handling
142 assert_options(ASSERT_ACTIVE , TRUE);
143 assert_options(ASSERT_WARNING , FALSE);
144 assert_options(ASSERT_BAIL , TRUE);
145 assert_options(ASSERT_QUIET_EVAL, FALSE);
147 // Set assertion handler
148 assert_options(ASSERT_CALLBACK , '__assertHandler');