]> git.mxchange.org Git - shipsimu.git/blob - application/shipsimu/exceptions.php
Continued:
[shipsimu.git] / application / shipsimu / 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 - 2022 Ship-Simu 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 shipsimu_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                 // Generate exception code
38                 $exceptionCode = ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode()));
39
40                 // Get all call levels from backtrace
41                 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
42                         // Init argument string
43                         $argsString = '';
44
45                         // Arguments given?
46                         if (isset($traceArray['args'])) {
47                                 // Convert arguments type into human-readable
48                                 foreach ($traceArray['args'] as $arg) {
49                                         $argsString .= ', ' . gettype($arg);
50                                 }
51                                 $argsString = substr($argsString, 2);
52                         }
53
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']  = '->';
59
60                         $backTrace .= sprintf("---------- Pos %d: ----------
61 Method : %s%s%s(%s)
62 ----- Caller: -----
63 File   : %s
64 Line   : %d\n",
65                                 ($idx + 1),
66                                 $traceArray['class'],
67                                 $traceArray['type'],
68                                 $traceArray['function'],
69                                 $argsString,
70                                 basename($traceArray['file']),
71                                 $traceArray['line']
72                         );
73                 }
74
75                 // Construct the message
76                 $message = sprintf("--------------------------------------------------------------------------------
77 Uncaught Exception : %s
78 --------------------------------------------------------------------------------
79 Message            : %s
80 Code               : %s
81 File               : %s
82 Line               : %d
83 --------------------------------------------------------------------------------
84 Backtrace:
85 --------------------------------------------------------------------------------
86 %s
87 --------------------------------------------------------------------------------\n",
88                         trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
89                         trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
90                         $exceptionCode,
91                         $exceptionInstance->getFile(),
92                         $exceptionInstance->getLine(),
93                         trim($backTrace)
94                 );
95
96                 // Output the message
97                 print($message);
98
99                 // Exit with code
100                 exit(hexdec($exceptionCode));
101         } elseif (is_object($exceptionInstance)) {
102                 // Output more details
103                 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
104                 exit(255);
105         } else {
106                 /*
107                  * Invalid exception instance detected! Do *only* throw exceptions that
108                  * extends our own exception 'FrameworkException' to get such nice
109                  * outputs like above.
110                  */
111                 printf('exceptionInstance[]=%s is invalid! Please inform the shipsimu developer team.' . PHP_EOL, gettype($exceptionInstance));
112                 exit(255);
113         }
114 }
115
116 // Error handler
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',
120                 basename($errfile),
121                 $errline,
122                 $errno,
123                 $errstr
124         );
125
126         // Throw an exception here
127         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
128 }
129
130 // Assertion handler
131 function shipsimu_assert_handler (string $file, int $line, int $code) {
132         // Empty code?
133         if (empty($code)) {
134                 $code = '<em>Unknown</em>';
135         }
136
137         // Create message
138         $message = sprintf('File: %s, Line: %s, Code: %s',
139                 basename($file),
140                 $line,
141                 $code
142         );
143
144         // Log assert
145         syslog(LOG_WARNING, $message);
146
147         // Throw an exception here
148         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
149 }
150
151 // Set error handler
152 //set_error_handler('shipsimu_error_handler');
153
154 // Set the new handler
155 set_exception_handler('shipsimu_exception_handler');
156
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');