Skip to content

Commit

Permalink
fix: swoft-cloud/swoft#1064 cannot overrid default erorr level types.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 25, 2019
1 parent f6c9dc2 commit 4c78aeb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/error/src/DefaultErrorDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __construct()
{
// Add default handler
$this->defaultHandler = new DefaultExceptionHandler();
}

public function init(): void
{
// Register system error handle
$this->registerErrorHandle();
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/src/DefaultExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DefaultExceptionHandler implements DefaultErrorHandlerInterface
*/
public function handle(Throwable $e): void
{
// Log::error($e->getMessage());
// Log::error($e->getMessage()); // maybe not in co env.
$error = PhpHelper::exceptionToString($e, 'DEFAULT HANDLER', true);

CLog::error($error);
Expand Down
17 changes: 15 additions & 2 deletions src/error/src/ErrorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ public function addHandler(string $exceptionClass, string $handlerClass, int $ty
}

/**
* Match error handler by type and exception object.
*
* @param Throwable $e
* @param int $type
*
* @return mixed|null If match ok, will return handler object.
*/
public function matchHandler(Throwable $e, int $type = ErrorType::DEF)
public function match(Throwable $e, int $type = ErrorType::DEF)
{
// No handlers found
if (!isset($this->handlers[$type]) || $this->getCount() === 0) {
Expand All @@ -65,7 +67,6 @@ public function matchHandler(Throwable $e, int $type = ErrorType::DEF)
}

$handler = null;

foreach ($handlers as $exceptionClass => $handlerClass) {
if ($e instanceof $exceptionClass) {
$handler = Swoft::getSingleton($handlerClass);
Expand All @@ -76,6 +77,18 @@ public function matchHandler(Throwable $e, int $type = ErrorType::DEF)
return $handler;
}

/**
* @param Throwable $e
* @param int $type
*
* @return mixed|null If match ok, will return handler object.
* @deprecated please use match() instead.
*/
public function matchHandler(Throwable $e, int $type = ErrorType::DEF)
{
return $this->match($e, $type);
}

/**
* @return int
*/
Expand Down
16 changes: 13 additions & 3 deletions src/error/test/unit/ErrorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use LogicException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Swoft\Error\DefaultExceptionHandler;
use Swoft\Error\ErrorManager;
use Swoft\Error\ErrorType;
use SwoftTest\Error\Testing\CustomErrorHandler;

/**
Expand All @@ -19,23 +21,31 @@ public function testGeneric(): void
$eh = new ErrorManager();

// null
$ret = $eh->matchHandler($e);
$ret = $eh->match($e);
$this->assertNull($ret);

$eh->addHandler(RuntimeException::class, CustomErrorHandler::class);

$this->assertTrue($eh->getCount() > 0);
$this->assertTrue($eh->getTypeCount() > 0);

$ret = $eh->matchHandler($e);
$ret = $eh->match($e);
$this->assertNotEmpty($ret);

$e1 = new LogicException('logic error');
$ret = $eh->matchHandler($e1);
$ret = $eh->match($e1);
$this->assertNull($ret);

$eh->clear();

$this->assertSame(0, $eh->getCount());
}

public function testDefHandler(): void
{
$deh = new DefaultExceptionHandler();

$this->assertSame(ErrorType::DEF, $deh->getType());
$this->assertSame(ErrorType::DEFAULT, $deh->getType());
}
}

0 comments on commit 4c78aeb

Please sign in to comment.