]> git.mxchange.org Git - core.git/blob - application/tests/exceptions.php
Continued:
[core.git] / application / tests / exceptions.php
1 <?php
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;
7
8 /**
9  * An include file for setting up the exception handler of test suite
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  *
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.
20  *
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.
25  *
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/>.
28  */
29
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)) {
34                 // Init variable
35                 $backTrace = '';
36
37                 // Get all call levels from backtrace
38                 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
39                         // Init argument string
40                         $argsString = '';
41
42                         // Arguments given?
43                         if (isset($traceArray['args'])) {
44                                 // Convert arguments type into human-readable
45                                 foreach ($traceArray['args'] as $arg) {
46                                         $argsString .= ', ' . gettype($arg);
47                                 }
48                                 $argsString = substr($argsString, 2);
49                         }
50
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']  = '->';
56
57                         $backTrace .= sprintf("---------- Pos %d: ----------
58 Method : %s%s%s(%s)
59 ----- Caller: -----
60 File   : %s
61 Line   : %d\n",
62                                 ($idx + 1),
63                                 $traceArray['class'],
64                                 $traceArray['type'],
65                                 $traceArray['function'],
66                                 $argsString,
67                                 basename($traceArray['file']),
68                                 $traceArray['line']
69                         );
70                 }
71
72                 // Construct the message
73                 $message = sprintf("--------------------------------------------------------------------------------
74 Uncaught Exception : %s
75 --------------------------------------------------------------------------------
76 Message            : %s
77 Code               : %s
78 File               : %s
79 Line               : %d
80 --------------------------------------------------------------------------------
81 Backtrace:
82 --------------------------------------------------------------------------------
83 %s
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(),
90                         trim($backTrace)
91                 );
92
93                 // Output the message
94                 print($message);
95         } elseif (is_object($exceptionInstance)) {
96                 // Output more details
97                 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
98         } else {
99                 /*
100                  * Invalid exception instance detected! Do *only* throw exceptions that
101                  * extends our own exception 'FrameworkException' to get such nice
102                  * outputs like above.
103                  */
104                 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
105         }
106 }
107
108 // Error handler
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',
112                 basename($errfile),
113                 $errline,
114                 $errno,
115                 $errstr
116         );
117
118         // Throw an exception here
119         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
120 }
121
122 // Assertion handler
123 function core_assert_handler (string $file, int $line, int $code) {
124         // Empty code?
125         if (empty($code)) {
126                 $code = '<em>Unknown</em>';
127         }
128
129         // Create message
130         $message = sprintf('File: %s, Line: %s, Code: %s',
131                 basename($file),
132                 $line,
133                 $code
134         );
135
136         // Log assert
137         syslog(LOG_WARNING, $message);
138
139         // Throw an exception here
140         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
141 }
142
143 // Set error handler
144 //set_error_handler('core_error_handler');
145
146 // Set the new handler
147 set_exception_handler('core_exception_handler');
148
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');