-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from bearsunday/throwable-handler
Add Throwable handler
- Loading branch information
Showing
6 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Sunday\Extension\Error; | ||
|
||
use BEAR\Sunday\Extension\Router\RouterMatch as Request; | ||
use Throwable; | ||
|
||
interface ThrowableHandlerInterface | ||
{ | ||
/** | ||
* Handle Throwable | ||
*/ | ||
public function handle(Throwable $e, Request $request) : self; | ||
|
||
/** | ||
* Transfer error page | ||
*/ | ||
public function transfer() : void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Sunday\Provide\Error; | ||
|
||
use BEAR\Sunday\Extension\Error\ErrorInterface; | ||
use BEAR\Sunday\Extension\Error\ThrowableHandlerInterface; | ||
use BEAR\Sunday\Extension\Router\RouterMatch as Request; | ||
use const E_ERROR; | ||
use Error; | ||
use ErrorException; | ||
use Exception; | ||
use Throwable; | ||
|
||
final class ThrowableHandler implements ThrowableHandlerInterface | ||
{ | ||
/** | ||
* @var ErrorInterface | ||
*/ | ||
private $error; | ||
|
||
public function __construct(ErrorInterface $error) | ||
{ | ||
$this->error = $error; | ||
} | ||
|
||
public function handle(Throwable $e, Request $request) : ThrowableHandlerInterface | ||
{ | ||
$e = $e instanceof Error ? new ErrorException($e->getMessage(), (int) $e->getCode(), E_ERROR, $e->getFile(), $e->getLine()) : $e; | ||
assert($e instanceof Exception); | ||
$this->error->handle($e, $request); | ||
|
||
return $this; | ||
} | ||
|
||
public function transfer() : void | ||
{ | ||
$this->error->transfer(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Sunday\Provide\Error; | ||
|
||
use BEAR\Resource\Exception\ResourceNotFoundException; | ||
use BEAR\Sunday\Extension\Router\RouterMatch; | ||
use BEAR\Sunday\Provide\Transfer\ConditionalResponse; | ||
use BEAR\Sunday\Provide\Transfer\FakeHttpResponder; | ||
use BEAR\Sunday\Provide\Transfer\Header; | ||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ThrowableHandlerTest extends TestCase | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
public static $code; | ||
|
||
/** | ||
* @var ThrowableHandler | ||
*/ | ||
private $throableHandler; | ||
|
||
protected function setUp() : void | ||
{ | ||
FakeHttpResponder::reset(); | ||
$this->throableHandler = new ThrowableHandler(new VndError(new FakeHttpResponder(new Header, new ConditionalResponse))); | ||
ini_set('error_log', '/dev/null'); | ||
} | ||
|
||
public function testException() : void | ||
{ | ||
$e = new ResourceNotFoundException('', 404); | ||
$this->throableHandler->handle($e, new RouterMatch)->transfer(); | ||
$this->assertSame(404, FakeHttpResponder::$code); | ||
$this->assertSame([['Content-Type: application/vnd.error+json', false]], FakeHttpResponder::$headers); | ||
$this->assertSame('{"message":"Not Found"}', FakeHttpResponder::$body); | ||
} | ||
|
||
public function testError() : void | ||
{ | ||
try { | ||
1 / 0; // @phpstan-ignore-line | ||
} catch (Exception $e) { | ||
} | ||
$this->throableHandler->handle($e, new RouterMatch)->transfer(); // @phpstan-ignore-line | ||
$this->assertSame(500, FakeHttpResponder::$code); | ||
$this->assertSame([['Content-Type: application/vnd.error+json', false]], FakeHttpResponder::$headers); | ||
$this->assertSame('{"message":"500 Server Error"}', FakeHttpResponder::$body); | ||
} | ||
} |