Skip to content

Commit

Permalink
Merge pull request #2 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fixed ob_clean() Bug + Improvments
  • Loading branch information
usernane committed May 17, 2022
2 parents c2cc6ea + 60db73f commit 8c813ae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/webfiori/error/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 7 additions & 2 deletions src/webfiori/error/ErrorHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/webfiori/error/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'];
Expand Down
15 changes: 14 additions & 1 deletion src/webfiori/error/TraceEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion tests/webfiori/tests/error/TraceEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
/**
Expand Down

0 comments on commit 8c813ae

Please sign in to comment.