2 // Import framework stuff
3 use Org\Mxchange\CoreFramework\Assertion\AssertionException;
4 use Org\Mxchange\CoreFramework\Error\FatalErrorException;
5 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 * An include file for setting up the exception handler of test suite
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 // The node's own exception handler
31 function core_exception_handler ($exceptionInstance) {
32 // Is it an object and a valid instance?
33 if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
37 // Get all call levels from backtrace
38 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
39 // Init argument string
43 if (isset($traceArray['args'])) {
44 // Convert arguments type into human-readable
45 foreach ($traceArray['args'] as $arg) {
46 $argsString .= ', ' . gettype($arg);
48 $argsString = substr($argsString, 2);
51 // Set missing file/line
52 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
53 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
54 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
55 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
57 $backTrace .= sprintf("---------- Pos %d: ----------
65 $traceArray['function'],
67 basename($traceArray['file']),
72 // Construct the message
73 $message = sprintf("--------------------------------------------------------------------------------
74 Uncaught Exception : %s
75 --------------------------------------------------------------------------------
80 --------------------------------------------------------------------------------
82 --------------------------------------------------------------------------------
84 --------------------------------------------------------------------------------\n",
85 trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
86 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
87 ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
88 $exceptionInstance->getFile(),
89 $exceptionInstance->getLine(),
95 } elseif (is_object($exceptionInstance)) {
96 // Output more details
97 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
100 * Invalid exception instance detected! Do *only* throw exceptions that
101 * extends our own exception 'FrameworkException' to get such nice
102 * outputs like above.
104 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
109 function core_error_handler (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
110 // Construct the message
111 $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s',
118 // Throw an exception here
119 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
123 function core_assert_handler (string $file, int $line, int $code) {
126 $code = '<em>Unknown</em>';
130 $message = sprintf('File: %s, Line: %s, Code: %s',
137 syslog(LOG_WARNING, $message);
139 // Throw an exception here
140 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
144 //set_error_handler('core_error_handler');
146 // Set the new handler
147 set_exception_handler('core_exception_handler');
149 // Init assert handling
150 assert_options(ASSERT_ACTIVE , true);
151 assert_options(ASSERT_WARNING , true);
152 assert_options(ASSERT_BAIL , true);
153 assert_options(ASSERT_QUIET_EVAL, false);
154 assert_options(ASSERT_CALLBACK , 'core_assert_handler');