]> git.mxchange.org Git - shipsimu.git/blobdiff - application/shipsimu/exceptions.php
Continued:
[shipsimu.git] / application / shipsimu / exceptions.php
index 8e4e14ff95c40d770bfdbf11176425225b50392c..28c8fea1bc43f95b0268ef00e4f0cc90060dde6e 100644 (file)
@@ -1,12 +1,17 @@
 <?php
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Assertion\AssertionException;
+use Org\Mxchange\CoreFramework\Error\FatalErrorException;
+use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
 /**
- * The exception handler for this application
+ * An include file for setting up the exception handler of test suite
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Ship-Simu Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-// Our own exception handler
-function __exceptionHandler (FrameworkException $e) {
-       // Call the app_exit() method
-       ApplicationEntryPoint::app_exit(sprintf("[Main:] The application <span class=\"app_name\">%s</span> (<span class=\"app_short_name\">%s</span>) has terminated due to an uncaught exception: <span class=\"exception_name\">%s</span> <span class=\"exception_number\">[%s]</span>: <span class=\"debug_exception\">%s</span> Backtrace: <div class=\"debug_backtrace\">%s</div>",
-               ApplicationHelper::getSelfInstance()->getAppName(),
-               ApplicationHelper::getSelfInstance()->getAppShortName(),
-               $e->__toString(),
-               $e->getHexCode(),
-               $e->getMessage(),
-               $e->getPrintableBackTrace()
-       ),
-               $e->getHexCode(),
-               $e->getExtraData()
-       );
-} // END - function
+// The node's own exception handler
+function shipsimu_exception_handler ($exceptionInstance) {
+       // Is it an object and a valid instance?
+       if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
+               // Init variable
+               $backTrace = '';
 
-// Set the new handler
-set_exception_handler('__exceptionHandler');
+               // Get all call levels from backtrace
+               foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
+                       // Init argument string
+                       $argsString = '';
+
+                       // Arguments given?
+                       if (isset($traceArray['args'])) {
+                               // Convert arguments type into human-readable
+                               foreach ($traceArray['args'] as $arg) {
+                                       $argsString .= ', ' . gettype($arg);
+                               }
+                               $argsString = substr($argsString, 2);
+                       }
+
+                       // Set missing file/line
+                       if (!isset($traceArray['file']))  $traceArray['file']  = 'unknown';
+                       if (!isset($traceArray['line']))  $traceArray['line']  = '0';
+                       if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
+                       if (!isset($traceArray['type']))  $traceArray['type']  = '->';
+
+                       $backTrace .= sprintf("---------- Pos %d: ----------
+Method : %s%s%s(%s)
+----- Caller: -----
+File   : %s
+Line   : %d\n",
+                               ($idx + 1),
+                               $traceArray['class'],
+                               $traceArray['type'],
+                               $traceArray['function'],
+                               $argsString,
+                               basename($traceArray['file']),
+                               $traceArray['line']
+                       );
+               }
+
+               // Construct the message
+               $message = sprintf("--------------------------------------------------------------------------------
+Uncaught Exception : %s
+--------------------------------------------------------------------------------
+Message            : %s
+Code               : %s
+File               : %s
+Line               : %d
+--------------------------------------------------------------------------------
+Backtrace:
+--------------------------------------------------------------------------------
+%s
+--------------------------------------------------------------------------------\n",
+                       trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
+                       trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
+                       ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
+                       $exceptionInstance->getFile(),
+                       $exceptionInstance->getLine(),
+                       trim($backTrace)
+               );
+
+               // Output the message
+               print($message);
+       } elseif (is_object($exceptionInstance)) {
+               // Output more details
+               printf('exceptionInstance=%s', print_r($exceptionInstance, true));
+       } else {
+               /*
+                * Invalid exception instance detected! Do *only* throw exceptions that
+                * extends our own exception 'FrameworkException' to get such nice
+                * outputs like above.
+                */
+               printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
+       }
+}
 
 // Error handler
-function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
+function shipsimu_error_handler (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
        // Construct the message
-       $message = sprintf("File: <span class=\"debug_file\">%s</span>, Line: <span class=\"debug_line\">%s</span>, Code: <span class=\"debug_code\">%s</span>, Message: <span class=\"debug_message\">%s</span>",
+       $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s',
                basename($errfile),
                $errline,
                $errno,
@@ -53,33 +117,38 @@ function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext)
 
        // Throw an exception here
        throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
-} // END - function
-
-// Set error handler
-set_error_handler('__errorHandler');
+}
 
 // Assertion handler
-function __assertHandler ($file, $line, $code) {
+function shipsimu_assert_handler (string $file, int $line, int $code) {
        // Empty code?
-       if ($code === "") $code = "<em>Unknown</em>";
+       if (empty($code)) {
+               $code = '<em>Unknown</em>';
+       }
 
        // Create message
-       $message = sprintf("File: <span class=\"debug_file\">%s</span>, Line: <span class=\"debug_line\">%s</span>, Code: <span class=\"debug_code\">%s</span>",
+       $message = sprintf('File: %s, Line: %d, Code: %d',
                basename($file),
                $line,
                $code
        );
 
+       // Log assert
+       syslog(LOG_WARNING, $message);
+
        // Throw an exception here
        throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
-} // END - function
+}
+
+// Set error handler
+//set_error_handler('shipsimu_error_handler');
+
+// Set the new handler
+set_exception_handler('shipsimu_exception_handler');
 
 // Init assert handling
-assert_options(ASSERT_ACTIVE,     1);
-assert_options(ASSERT_WARNING,    0);
-assert_options(ASSERT_BAIL,       0);
-assert_options(ASSERT_QUIET_EVAL, 0);
-assert_options(ASSERT_CALLBACK,   '__assertHandler');
-
-// [EOF]
-?>
+assert_options(ASSERT_ACTIVE    , true);
+assert_options(ASSERT_WARNING   , true);
+assert_options(ASSERT_BAIL      , true);
+assert_options(ASSERT_QUIET_EVAL, false);
+assert_options(ASSERT_CALLBACK  , 'shipsimu_assert_handler');