From 5b4b66ee8bf72d77634692b75db9c235f9d1f186 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 22 Mar 2024 22:54:13 -0300 Subject: [PATCH] More Exceptions --- system/Exception/Error.php | 67 +++++++++++++++++++ system/Exception/ErrorException.php | 62 +++++++++++++++++ system/Exception/TypeError.php | 22 ++++++ .../MagiQL/Manipulation/AbstractBaseQuery.php | 4 +- .../Connectors/Oracle/ORDS/Model/Query.php | 3 +- system/engine/exception.php | 2 + system/engine/restful.php | 22 ++++-- 7 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 system/Exception/Error.php create mode 100644 system/Exception/ErrorException.php create mode 100644 system/Exception/TypeError.php diff --git a/system/Exception/Error.php b/system/Exception/Error.php new file mode 100644 index 0000000..0610a9c --- /dev/null +++ b/system/Exception/Error.php @@ -0,0 +1,67 @@ +severity; + } + + /** @return int */ + public function setSeverity($severity = E_ERROR) { + return $this->severity = $severity; + } + + /** + * Save the exceptions in the exceptions log file + * @return void + */ + public function __destruct() + { + $debugging = (\Phacil\Framework\Config::DEBUG()) ?: false; + $this->errorFormat = \Phacil\Framework\Config::DEBUG_FORMAT() ?: $this->errorFormat; + + $log = new \Phacil\Framework\Log($this->exceptionFile); + + $errorStamp = [ + 'message' => $this->getMessage(), + 'line' => $this->getLine(), + 'severity' => $this->getSeverity(), + 'file' => $this->getFile(), + 'trace' => ($debugging) ? (($this->errorFormat == 'json') ? ($this->heritageTrace ?: $this->getTrace()) : \Phacil\Framework\Debug::trace(($this->heritageTrace ?: $this->getTrace()))) : null + ]; + + if ($this->getCode() > 0) + $errorStamp['code'] = $this->getCode(); + + $log->error(($this->errorFormat == 'json') ? json_encode($errorStamp) : implode(PHP_EOL, array_map( + [self::class, 'convertArray'], + $errorStamp, + array_keys($errorStamp) + ))); + } +} \ No newline at end of file diff --git a/system/Exception/ErrorException.php b/system/Exception/ErrorException.php new file mode 100644 index 0000000..ab6163f --- /dev/null +++ b/system/Exception/ErrorException.php @@ -0,0 +1,62 @@ +errorException = new \ErrorException($message, $code, $severity, $filename, $line, $previous); + parent::__construct($message, $code, $previous); + //$this->setObject($this->errorException); + if($filename) + $this->file = $filename; + + if($line) + $this->line = $line; + + $this->severity = $severity; + } + + /** @return int */ + final public function getSeverity() { + return $this->severity; + } +} \ No newline at end of file diff --git a/system/Exception/TypeError.php b/system/Exception/TypeError.php new file mode 100644 index 0000000..eefcd1a --- /dev/null +++ b/system/Exception/TypeError.php @@ -0,0 +1,22 @@ +getBuilder()->execute($this); - } catch (\Throwable $th) { - throw new \Phacil\Framework\Exception($th->getMessage()); + } catch (\Exception $th) { + throw new \Phacil\Framework\Exception($th->getMessage(), $th->getCode(), $th); } return []; } diff --git a/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php b/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php index 6c2b002..35e9dd4 100644 --- a/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php +++ b/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php @@ -9,13 +9,12 @@ namespace Phacil\Framework\Databases\Connectors\Oracle\ORDS\Model; -use Phacil\Framework\Databases\Connectors\Oracle\ORDS\Connector; use Phacil\Framework\Databases\Connectors\Oracle\ORDS\Api\HandleInterface; use Phacil\Framework\Databases\Connectors\Oracle\ORDS\Api\Query as QueryApi; use Phacil\Framework\Databases\Connectors\Oracle\ORDS\Helper\Data as DataHelper; class Query implements QueryApi { - + /** * * @var \Phacil\Framework\Databases\Connectors\Oracle\ORDS\Api\HandleInterface diff --git a/system/engine/exception.php b/system/engine/exception.php index 547ef4e..75453fe 100644 --- a/system/engine/exception.php +++ b/system/engine/exception.php @@ -26,6 +26,8 @@ class Exception extends \Exception implements \Phacil\Framework\Exception\Throwa protected $exceptionFile = self::DEFAULT_EXCEPTION_FILE; + public $logLevel = 'critical'; + /** * * @param \Exception $object diff --git a/system/engine/restful.php b/system/engine/restful.php index 3955afc..9383d65 100644 --- a/system/engine/restful.php +++ b/system/engine/restful.php @@ -152,10 +152,12 @@ abstract class RESTful extends Controller { new Exception($th->getMessage(), $th->getCode()); return $this->__callInterrupt($th->getMessage()); } else { - throw new Exception($th->getMessage(), $th->getCode()); + throw $th; } - } catch (Exception $e) { + } catch (\TypeError $e) { + throw new \Phacil\Framework\Exception\TypeError($e->getMessage(), $e->getCode(), $e); + } catch (\Exception $e) { throw new Exception($e->getMessage(), $e->getCode()); } } else { @@ -171,19 +173,25 @@ abstract class RESTful extends Controller { * @return bool */ static function __testType($type, $data){ - switch ($type) { case 'mixed': return true; break; - + + case 'integer': case 'int': + //return ctype_digit($data); + return filter_var($data, FILTER_VALIDATE_INT) !== false; + break; + + case 'double': + case 'float': + return filter_var($data, FILTER_VALIDATE_FLOAT) !== false; + break; + case 'string': case 'array': - case 'integer': case 'bool': - case 'double': - case 'float': case 'long': case 'null': case 'numeric':