diff --git a/src/webfiori/error/AbstractHandler.php b/src/webfiori/error/AbstractHandler.php index 7e12a47..ad98467 100644 --- a/src/webfiori/error/AbstractHandler.php +++ b/src/webfiori/error/AbstractHandler.php @@ -65,6 +65,14 @@ public function getName() : string { public function getClass() { return TraceEntry::extractClassName($this->getException()->getFile()); } + /** + * Returns exception error code. + * + * @return string Error code of the exception. + */ + public function getCode() : string { + return $this->getException()->getCode().''; + } /** * Returns an object that represents the exception which was thrown. * diff --git a/src/webfiori/error/ErrorHandlerException.php b/src/webfiori/error/ErrorHandlerException.php index af3d03e..b9bf070 100644 --- a/src/webfiori/error/ErrorHandlerException.php +++ b/src/webfiori/error/ErrorHandlerException.php @@ -25,12 +25,17 @@ public function __construct(string $message = "", int $code = 0, string $file = $line = null; for ($x = 0 ; $x < count($trace) ; $x++) { - if ($x != 0 && $line !== null) { + if ($x == 1) { + $line = isset($trace[$x]['line']) ? $trace[$x]['line'] : 'X'; + continue; + } + if ($x > 1) { $temp = $trace[$x]; $temp['line'] = $line; $this->debugTrace[] = new TraceEntry($temp); + $line = isset($trace[$x]['line']) ? $trace[$x]['line'] : 'X'; } - $line = isset($trace[$x]['line']) ? $trace[$x]['line'] : 'X'; + } $this->debugTrace[] = new TraceEntry([ 'file' => $file, diff --git a/src/webfiori/error/Handler.php b/src/webfiori/error/Handler.php index db3c800..9f00552 100644 --- a/src/webfiori/error/Handler.php +++ b/src/webfiori/error/Handler.php @@ -95,7 +95,7 @@ private function __construct() { { $errClass = TraceEntry::extractClassName($errfile); $errType = Handler::ERR_TYPES[$errno]; - $message = $errType['description'].': '.$errstr.' at '.$errClass.' Line '.$errline; + $message = 'An exception caused by an error. '.$errType['description'].': '.$errstr.' at '.$errClass.' Line '.$errline; throw new ErrorHandlerException($message, $errno, $errfile); }); set_exception_handler(function (Throwable $ex) @@ -113,7 +113,9 @@ private function __construct() { $lastErr = error_get_last(); if ($lastErr !== null) { - ob_clean(); + if (ob_get_length()) { + ob_clean(); + } $errClass = TraceEntry::extractClassName($lastErr['file']); $errType = Handler::ERR_TYPES[$lastErr['type']]; $message = $errType['description'].': '.$lastErr['message'].' At '.$errClass.' Line '.$lastErr['line']; diff --git a/src/webfiori/error/TraceEntry.php b/src/webfiori/error/TraceEntry.php index 770a49c..3b6a2d9 100644 --- a/src/webfiori/error/TraceEntry.php +++ b/src/webfiori/error/TraceEntry.php @@ -36,7 +36,20 @@ public function __construct(array $debugTraceEntry) { * @return string */ public function __toString() { - return 'At class '.$this->getClass().' line '.$this->getLine(); + $line = $this->getLine(); + $class = $this->getClass(); + + if ($class == 'X') { + $retVal = 'NO CLASS'; + } else { + $retVal = 'At class '.$this->getClass(); + } + + if ($line != 'X') { + $retVal .= ' line '.$line; + } + + return $retVal; } /** * Extract PHP's class name based on the file name of the class/ diff --git a/tests/webfiori/tests/error/TraceEntryTest.php b/tests/webfiori/tests/error/TraceEntryTest.php index 000a3f4..c28def3 100644 --- a/tests/webfiori/tests/error/TraceEntryTest.php +++ b/tests/webfiori/tests/error/TraceEntryTest.php @@ -14,7 +14,7 @@ class TraceEntryTest extends TestCase { */ public function test00() { $entry = new TraceEntry([]); - $this->assertEquals('At class X line X', $entry.''); + $this->assertEquals('NO CLASS', $entry.''); $this->assertTrue(true); } /**