Skip to content

Commit

Permalink
* fix Error class logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmondDantes committed Oct 27, 2024
1 parent ee4bd28 commit bb221fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
32 changes: 16 additions & 16 deletions src/Errors/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,50 @@ public static function createFromLastError(?array $error = null): ?BaseException
/**
* Errors factory.
*
* @param int $code Class of error
* @param string $message Message
* @param string $file File
* @param int $line Line
* @param int $severity Class of error
* @param string $message Message
* @param string $file File
* @param int $line Line
*
* @return BaseExceptionInterface
*/
public static function createError(int $code, string $message, string $file, int $line): BaseExceptionInterface
public static function createError(int $severity, string $message, string $file, int $line): BaseExceptionInterface
{
if (!\array_key_exists($code, self::$ERRORS)) {
$code = self::ERROR;
if (!\array_key_exists($severity, self::$ERRORS)) {
$severity = self::ERROR;
}

if (\in_array($code, [E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE])) {
return new UserError($code, $message, $file, $line);
if (\in_array($severity, [E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE])) {
return new UserError($severity, $message, $file, $line);
}

switch (self::$ERRORS[$code]) {
switch (self::$ERRORS[$severity]) {
case self::EMERGENCY :
{
//
// EMERGENCY created as fatal error
//
$err = new Error($code, $message, $file, $line);
$err = new Error($severity, $message, $file, $line);
$err->markAsFatal();

return $err;
}
case self::WARNING :
{
return new Warning($code, $message, $file, $line);
return new Warning($severity, $message, $file, $line);
}
case self::NOTICE :
case self::INFO :
case self::DEBUG :
{
return new Notice($code, $message, $file, $line);
return new Notice($severity, $message, $file, $line);
}
case self::ALERT :
case self::CRITICAL :
case self::ERROR :
default:
{
return new Error($code, $message, $file, $line);
return new Error($severity, $message, $file, $line);
}
}
}
Expand Down Expand Up @@ -175,11 +175,11 @@ public function isContainer(): bool
#[\Override]
public function getLevel(): int
{
if (!\array_key_exists($this->code, self::$ERRORS)) {
if (!\array_key_exists($this->getSeverity(), self::$ERRORS)) {
return self::ERROR;
}

return self::$ERRORS[$this->code];
return self::$ERRORS[$this->getSeverity()];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ public static function exceptionHandler(\Throwable $exception): void
*
*
*/
public static function errorHandler(int $code, string $message, string $file, int|string $line): bool
public static function errorHandler(int $severity, string $message, string $file, int|string $line): bool
{
self::registerException(
Errors\Error::createError($code, $message, $file, (int) $line)
Errors\Error::createError($severity, $message, $file, (int) $line)
);

/* Don't execute PHP internal error handler */
Expand Down
6 changes: 3 additions & 3 deletions tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,16 @@ public function testErrorHandler(): void
$this->assertTrue(\count($exceptions) === \count($errors), '$exceptions count must equal $errors');

foreach ($exceptions as $error) {
[$code, $level] = [\key($errors), \current($errors)];
[$severity, $level] = [\key($errors), \current($errors)];
\next($errors);

$this->assertInstanceOf(BaseExceptionInterface::class, $error);
$this->assertInstanceOf(Error::class, $error);
$this->assertEquals($code, $error->getCode(), '$error->getCode() failed');
$this->assertEquals($severity, $error->getSeverity(), '$error->getSeverity() failed');
$this->assertEquals(
$level,
$error->getLevel(),
'$error->get_level() failed (line: ' . $error->getLine() . ')'
'$error->getLevel() failed (line: ' . $error->getLine() . ')'
);
$this->assertInstanceOf(
'IfCastle\Exceptions\Errors\\' . $error->getMessage(),
Expand Down

0 comments on commit bb221fe

Please sign in to comment.