Skip to content

Commit

Permalink
Merge pull request #144 from bearsunday/throwable-handler
Browse files Browse the repository at this point in the history
Add Throwable handler
  • Loading branch information
koriym authored Jul 10, 2020
2 parents 55c39de + 945c14b commit 13cbef1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 3 deletions.
6 changes: 3 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="src"/>
<ignoreFiles>
<directory name="vendor" />
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MissingConstructor errorLevel="suppress" />
<MissingConstructor errorLevel="suppress"/>
</issueHandlers>
</psalm>
21 changes: 21 additions & 0 deletions src/Extension/Error/ThrowableHandlerInterface.php
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;
}
2 changes: 2 additions & 0 deletions src/Provide/Error/ErrorModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BEAR\Sunday\Provide\Error;

use BEAR\Sunday\Extension\Error\ErrorInterface;
use BEAR\Sunday\Extension\Error\ThrowableHandlerInterface;
use Ray\Di\AbstractModule;

class ErrorModule extends AbstractModule
Expand All @@ -15,5 +16,6 @@ class ErrorModule extends AbstractModule
protected function configure() : void
{
$this->bind(ErrorInterface::class)->to(VndError::class);
$this->bind(ThrowableHandlerInterface::class)->to(ThrowableHandler::class);
}
}
41 changes: 41 additions & 0 deletions src/Provide/Error/ThrowableHandler.php
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();
}
}
1 change: 1 addition & 0 deletions src/Provide/Transfer/HttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __invoke(ResourceObject $ro, array $server) : void
private function getOutput(ResourceObject $ro, array $server) : Output
{
$ro->toString(); // render and set headers

return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?: $ro->toString());
}
}
54 changes: 54 additions & 0 deletions tests/Provide/Error/ThrowableHandlerTest.php
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);
}
}

0 comments on commit 13cbef1

Please sign in to comment.