From d1e0816c032482bb9ea9da42fbfce0c307f09195 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sun, 10 Apr 2022 23:23:46 -0300 Subject: [PATCH] Changes on exceptions treatment calls --- system/database/autoload.php | 2 +- system/engine/autoload.php | 22 ++++++++-------------- system/engine/controller.php | 2 +- system/engine/front.php | 2 +- system/system.php | 35 ++++++++++++++++++----------------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/system/database/autoload.php b/system/database/autoload.php index cfd8c98..14b343d 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -47,7 +47,7 @@ final class Database { try { $this->createDriver(new $driverClass($hostname, $username, $password, $database)); } catch (Exception $th) { - throw new Exception('Error: Could not load database file ' . $driver . '!'); + throw new \Phacil\Framework\Exception($driver. ' not loaded. '.$th->getMessage(), $th->getCode()); } } diff --git a/system/engine/autoload.php b/system/engine/autoload.php index 6a1a6a9..1a31057 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -29,34 +29,28 @@ spl_autoload_register(function ($class) { try { class_alias("\\Phacil\\Framework\\".$class, $class); } catch (\Exception $th) { - $log = new \Phacil\Framework\Log("exception.log"); - $log->write($class.' not loaded!'); + throw new \Phacil\Framework\Exception ($th->getMessage()); } - //eval("class ".$class." extends \\Phacil\\Framework\\".$class." {}"); return; } $classNative = ($namespace[0] == "Phacil") ? str_replace('phacil\\framework\\', '', strtolower( $class)) : $class; if($namespace[0] == 'Phacil' && isset($namespace[2]) && $namespace[2] == 'Databases'){ - if(!defined('DIR_DATABASE')) - define('DIR_DATABASE', DIR_SYSTEM."database/"); - - $fileDB = DIR_DATABASE . str_replace("\\", "/", $classNative).'.php'; + $fileDB = \Phacil\Framework\Config::DIR_DATABASE(\Phacil\Framework\Config::DIR_SYSTEM() . "database/") . str_replace("\\", "/", $classNative).'.php'; try { - if (!file_exists($fileDB)) - throw new Exception ($fileDB.' does not exist'); - else + if (!file_exists($fileDB)){ + throw new \Phacil\Framework\Exception($fileDB.' does not exist', 2); + }else{ require_once($fileDB); - return; + return; + } } catch (Exception $th) { - $log = new \Phacil\Framework\Log("exception.log"); - $log->write($th->getMessage()); - + throw new \Phacil\Framework\Exception($th->getMessage(), $th->getCode(), $th); } } diff --git a/system/engine/controller.php b/system/engine/controller.php index d1766f2..85d2e97 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -211,7 +211,7 @@ abstract class Controller { break; } - } catch (\Throwable $th) { + } catch (Exception $th) { //throw $th; } } diff --git a/system/engine/front.php b/system/engine/front.php index 0a931ae..e089cf8 100644 --- a/system/engine/front.php +++ b/system/engine/front.php @@ -117,7 +117,7 @@ final class Front implements frontinterface { $this->error = ''; - throw new Exception("The controller can't be loaded", 1); + throw new Exception("The controller can't be loaded: ".$th->getMessage(), $th->getCode(), $th); } diff --git a/system/system.php b/system/system.php index cc698b9..9463374 100644 --- a/system/system.php +++ b/system/system.php @@ -344,6 +344,23 @@ $engine->load = new Loader($engine->registry); /** @var Config */ $engine->config = new Config(); +// Exception Handler +set_exception_handler(function ($e) use (&$engine) { + if ($engine->config->get('config_error_display')) { + echo '

' . get_class($e) . ': ' . $e->getMessage() . ' in ' . str_replace(\Phacil\Framework\Config::DIR_APPLICATION(), '', $e->getFile()) . ' on line ' . $e->getLine() . '

'; + } + + if (get_class($e) != 'Phacil\Framework\Exception') { + $exception = new \Phacil\Framework\Exception(); + $exception->setObject($e); + } + + + if ($engine->config->get('config_error_log')) { + $engine->log->write(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine()); + } +}); + if(defined('DB_DRIVER')) $engine->db = new Database(\Phacil\Framework\Config::DB_DRIVER(), \Phacil\Framework\Config::DB_HOSTNAME(), \Phacil\Framework\Config::DB_USERNAME(), \Phacil\Framework\Config::DB_PASSWORD(), \Phacil\Framework\Config::DB_DATABASE()); @@ -397,7 +414,7 @@ if(!$engine->config->get('config_error_filename')){ $engine->log = new Log($engine->config->get('config_error_filename')); // Error Handler -set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($engine){ +set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$engine){ switch ($errno) { case E_NOTICE: @@ -432,22 +449,6 @@ set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($engine){ return true; }); -set_exception_handler(function($e) use ($engine) { - if ($engine->config->get('config_error_display')) { - echo '

' . get_class($e) . ': ' . $e->getMessage() . ' in ' .str_replace(\Phacil\Framework\Config::DIR_APPLICATION(), '', $e->getFile()) . ' on line ' . $e->getLine() . '

'; - } - - if(get_class($e) != 'Phacil\Framework\Exception'){ - $exception = new \Phacil\Framework\Exception(); - $exception->setObject($e); - } - - - if ($engine->config->get('config_error_log')) { - $engine->log->write(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine()); - } -}); - /** * Caches * @var Caches