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 - 2022 Ship-Simu 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 shipsimu_exception_handler ($exceptionInstance) {
32 // Is it an object and a valid instance?
33 if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
37 // Generate exception code
38 $exceptionCode = ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode()));
40 // Get all call levels from backtrace
41 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
42 // Init argument string
46 if (isset($traceArray['args'])) {
47 // Convert arguments type into human-readable
48 foreach ($traceArray['args'] as $arg) {
49 $argsString .= ', ' . gettype($arg);
51 $argsString = substr($argsString, 2);
54 // Set missing file/line
55 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
56 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
57 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
58 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
60 $backTrace .= sprintf("---------- Pos %d: ----------
68 $traceArray['function'],
70 basename($traceArray['file']),
75 // Construct the message
76 $message = sprintf("--------------------------------------------------------------------------------
77 Uncaught Exception : %s
78 --------------------------------------------------------------------------------
83 --------------------------------------------------------------------------------
85 --------------------------------------------------------------------------------
87 --------------------------------------------------------------------------------\n",
88 trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
89 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
91 $exceptionInstance->getFile(),
92 $exceptionInstance->getLine(),
100 exit(hexdec($exceptionCode));
101 } elseif (is_object($exceptionInstance)) {
102 // Output more details
103 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
107 * Invalid exception instance detected! Do *only* throw exceptions that
108 * extends our own exception 'FrameworkException' to get such nice
109 * outputs like above.
111 printf('exceptionInstance[]=%s is invalid! Please inform the shipsimu developer team.' . PHP_EOL, gettype($exceptionInstance));
117 function shipsimu_error_handler (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
118 // Construct the message
119 $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s',
126 // Throw an exception here
127 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
131 function shipsimu_assert_handler (string $file, int $line, int $code) {
134 $code = '<em>Unknown</em>';
138 $message = sprintf('File: %s, Line: %s, Code: %s',
145 syslog(LOG_WARNING, $message);
147 // Throw an exception here
148 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
152 //set_error_handler('shipsimu_error_handler');
154 // Set the new handler
155 set_exception_handler('shipsimu_exception_handler');
157 // Init assert handling
158 assert_options(ASSERT_ACTIVE , true);
159 assert_options(ASSERT_WARNING , true);
160 assert_options(ASSERT_BAIL , true);
161 assert_options(ASSERT_QUIET_EVAL, false);
162 assert_options(ASSERT_CALLBACK , 'shipsimu_assert_handler');