mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Fixed the next request will be effected after using stopPropagation. (#571)
* Create Propagation.php * Fixed stopPropagation will effect the next request. * Added testing. * Update phpunit.xml * Update CHANGELOG.md
This commit is contained in:
parent
f46aa5d834
commit
67eb4af5c8
@ -4,6 +4,10 @@
|
||||
|
||||
- [#565](https://github.com/hyperf-cloud/hyperf/pull/565) Added options config for redis.
|
||||
|
||||
## Fixed
|
||||
|
||||
- [#571](https://github.com/hyperf-cloud/hyperf/pull/571) Fixed the next request will be effected after using stopPropagation.
|
||||
|
||||
# v1.0.15 - 2019-09-11
|
||||
|
||||
## Fixed
|
||||
|
@ -187,6 +187,7 @@
|
||||
"HyperfTest\\Elasticsearch\\": "src/elasticsearch/tests/",
|
||||
"HyperfTest\\Etcd\\": "src/etcd/tests/",
|
||||
"HyperfTest\\Event\\": "src/event/tests/",
|
||||
"HyperfTest\\ExceptionHandler\\": "src/exception-handler/tests/",
|
||||
"HyperfTest\\GrpcClient\\": "src/grpc-client/tests/",
|
||||
"HyperfTest\\GrpcServer\\": "src/grpc-server/tests/",
|
||||
"HyperfTest\\Guzzle\\": "src/guzzle/tests/",
|
||||
|
@ -22,6 +22,7 @@
|
||||
<directory suffix="Test.php">./src/dispatcher/tests</directory>
|
||||
<directory suffix="Test.php">./src/elasticsearch/tests</directory>
|
||||
<directory suffix="Test.php">./src/event/tests</directory>
|
||||
<directory suffix="Test.php">./src/exception-handler/tests</directory>
|
||||
<directory suffix="Test.php">./src/grpc-client/tests</directory>
|
||||
<directory suffix="Test.php">./src/grpc-server/tests</directory>
|
||||
<directory suffix="Test.php">./src/guzzle/tests</directory>
|
||||
|
1
src/exception-handler/.gitattributes
vendored
Normal file
1
src/exception-handler/.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
/tests export-ignore
|
@ -15,6 +15,7 @@
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"HyperfTest\\ExceptionHandler\\": "tests/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
|
@ -17,13 +17,6 @@ use Throwable;
|
||||
|
||||
abstract class ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* Determine if the exception should propagate to next handler.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $propagationStopped = false;
|
||||
|
||||
/**
|
||||
* Handle the exception, and return the specified result.
|
||||
*/
|
||||
@ -43,8 +36,8 @@ abstract class ExceptionHandler
|
||||
*/
|
||||
public function stopPropagation(): bool
|
||||
{
|
||||
$this->propagationStopped = true;
|
||||
return $this->propagationStopped;
|
||||
Propagation::instance()->setPropagationStopped(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,6 +47,6 @@ abstract class ExceptionHandler
|
||||
*/
|
||||
public function isPropagationStopped(): bool
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
return Propagation::instance()->isPropagationStopped();
|
||||
}
|
||||
}
|
||||
|
38
src/exception-handler/src/Propagation.php
Normal file
38
src/exception-handler/src/Propagation.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\ExceptionHandler;
|
||||
|
||||
use Hyperf\Utils\Traits\StaticInstance;
|
||||
|
||||
class Propagation
|
||||
{
|
||||
use StaticInstance;
|
||||
|
||||
/**
|
||||
* Determine if the exception should propagate to next handler.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $propagationStopped = false;
|
||||
|
||||
public function isPropagationStopped(): bool
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
}
|
||||
|
||||
public function setPropagationStopped(bool $propagationStopped): Propagation
|
||||
{
|
||||
$this->propagationStopped = $propagationStopped;
|
||||
return $this;
|
||||
}
|
||||
}
|
90
src/exception-handler/tests/ExceptionHandlerTest.php
Normal file
90
src/exception-handler/tests/ExceptionHandlerTest.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\ExceptionHandler;
|
||||
|
||||
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
|
||||
use Hyperf\HttpMessage\Base\Response;
|
||||
use Hyperf\Utils\Context;
|
||||
use HyperfTest\ExceptionHandler\Stub\BarExceptionHandler;
|
||||
use HyperfTest\ExceptionHandler\Stub\FooExceptionHandler;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ExceptionHandlerTest extends TestCase
|
||||
{
|
||||
protected function tearDown()
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
Context::set('test.exception-handler.latest-handler', null);
|
||||
}
|
||||
|
||||
public function testStopPropagation()
|
||||
{
|
||||
$handlers = [
|
||||
BarExceptionHandler::class,
|
||||
FooExceptionHandler::class,
|
||||
];
|
||||
|
||||
$container = $this->getContainer();
|
||||
|
||||
parallel([function () use ($container, $handlers) {
|
||||
$exception = new \Exception('xxx', 500);
|
||||
|
||||
Context::set(ResponseInterface::class, new Response());
|
||||
|
||||
$dispatcher = new ExceptionHandlerDispatcher($container);
|
||||
$dispatcher->dispatch($exception, $handlers);
|
||||
|
||||
$this->assertSame(FooExceptionHandler::class, Context::get('test.exception-handler.latest-handler'));
|
||||
}]);
|
||||
|
||||
parallel([function () use ($container, $handlers) {
|
||||
$exception = new \Exception('xxx', 0);
|
||||
|
||||
Context::set(ResponseInterface::class, new Response());
|
||||
|
||||
$dispatcher = new ExceptionHandlerDispatcher($container);
|
||||
$dispatcher->dispatch($exception, $handlers);
|
||||
|
||||
$this->assertSame(BarExceptionHandler::class, Context::get('test.exception-handler.latest-handler'));
|
||||
}]);
|
||||
|
||||
parallel([function () use ($container, $handlers) {
|
||||
$exception = new \Exception('xxx', 500);
|
||||
|
||||
Context::set(ResponseInterface::class, new Response());
|
||||
|
||||
$dispatcher = new ExceptionHandlerDispatcher($container);
|
||||
$dispatcher->dispatch($exception, $handlers);
|
||||
|
||||
$this->assertSame(FooExceptionHandler::class, Context::get('test.exception-handler.latest-handler'));
|
||||
}]);
|
||||
}
|
||||
|
||||
protected function getContainer()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
$container->shouldReceive('has')->andReturn(true);
|
||||
$container->shouldReceive('get')->with(BarExceptionHandler::class)->andReturn(new BarExceptionHandler());
|
||||
$container->shouldReceive('get')->with(FooExceptionHandler::class)->andReturn(new FooExceptionHandler());
|
||||
|
||||
return $container;
|
||||
}
|
||||
}
|
37
src/exception-handler/tests/Stub/BarExceptionHandler.php
Normal file
37
src/exception-handler/tests/Stub/BarExceptionHandler.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\ExceptionHandler\Stub;
|
||||
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\Utils\Context;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class BarExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
Context::set('test.exception-handler.latest-handler', static::class);
|
||||
|
||||
if ($throwable->getCode() === 0) {
|
||||
$this->stopPropagation();
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
32
src/exception-handler/tests/Stub/FooExceptionHandler.php
Normal file
32
src/exception-handler/tests/Stub/FooExceptionHandler.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\ExceptionHandler\Stub;
|
||||
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\Utils\Context;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class FooExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
Context::set('test.exception-handler.latest-handler', static::class);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user