diff --git a/src/Provide/Error/ExceptionAsString.php b/src/Provide/Error/ExceptionAsString.php index a277ba18..5ec4c55e 100644 --- a/src/Provide/Error/ExceptionAsString.php +++ b/src/Provide/Error/ExceptionAsString.php @@ -44,6 +44,7 @@ private function getPhpVariables(array $server) if (PHP_SAPI === 'cli') { return ''; } + return sprintf("\nPHP Variables\n\n\$_SERVER => %s", print_r($server, true)); } } diff --git a/src/Provide/Error/VndError.php b/src/Provide/Error/VndErrorHandler.php similarity index 84% rename from src/Provide/Error/VndError.php rename to src/Provide/Error/VndErrorHandler.php index 112d8935..27c6596d 100644 --- a/src/Provide/Error/VndError.php +++ b/src/Provide/Error/VndErrorHandler.php @@ -7,14 +7,13 @@ namespace BEAR\Package\Provide\Error; use BEAR\AppMeta\AbstractAppMeta; +use BEAR\Package\Provide\Error\ErrorPage as CliErrorPage; use BEAR\Resource\Code; use BEAR\Resource\Exception\BadRequestException as BadRequest; use BEAR\Resource\Exception\ResourceNotFoundException as NotFound; -use BEAR\Resource\Exception\ServerErrorException; use BEAR\Sunday\Extension\Error\ErrorInterface; use BEAR\Sunday\Extension\Router\RouterMatch as Request; use BEAR\Sunday\Extension\Transfer\TransferInterface; -use BEAR\Package\Provide\Error\ErrorPage as CliErrorPage; use BEAR\Sunday\Provide\Error\ErrorPage; /** @@ -22,7 +21,7 @@ * * @see https://github.com/blongden/vnd.error */ -class VndError implements ErrorInterface +class VndErrorHandler implements ErrorInterface { /** * @var int @@ -77,10 +76,11 @@ public function __construct(AbstractAppMeta $appMeta, TransferInterface $respond */ public function handle(\Exception $e, Request $request) { - $this->errorPage = PHP_SAPI === 'cli' ? new CliErrorPage($this->exceptionString->summery($e, $this->lastErrorFile)) : new ErrorPage; - $isCodeError = ($e instanceof NotFound || $e instanceof BadRequest || $e instanceof ServerErrorException); + $isCodeError = ($e instanceof NotFound || $e instanceof BadRequest); + $this->errorPage = $this->getErrorPage($e, $this->lastErrorFile); if ($isCodeError) { list($this->code, $this->body) = $this->codeError($e); + $this->log($e, $request); return $this; } @@ -95,12 +95,23 @@ public function handle(\Exception $e, Request $request) return $this; } + /** + * @param \Exception $e + * @param string $lastErrorFile + * + * @return \BEAR\Package\Provide\Error\ErrorPage|ErrorPage + */ + private function getErrorPage(\Exception $e, $lastErrorFile) + { + return PHP_SAPI === 'cli' ? new CliErrorPage($this->exceptionString->summery($e, $lastErrorFile)) : new ErrorPage; + } + /** * {@inheritdoc} */ public function transfer() { - $ro =$this->errorPage; + $ro = $this->errorPage; $ro->code = $this->code; $ro->headers['content-type'] = 'application/vnd.error+json'; $ro->body = $this->body; @@ -123,7 +134,7 @@ private function codeError(\Exception $e) /** * @param \Exception $e - * @param Request $request + * @param Request $request * * @return int logRef */ diff --git a/src/Provide/Error/VndErrorModule.php b/src/Provide/Error/VndErrorModule.php index 2e88b418..449c98d8 100644 --- a/src/Provide/Error/VndErrorModule.php +++ b/src/Provide/Error/VndErrorModule.php @@ -13,6 +13,6 @@ class VndErrorModule extends AbstractModule { protected function configure() { - $this->bind(ErrorInterface::class)->to(VndError::class); + $this->bind(ErrorInterface::class)->to(VndErrorHandler::class); } } diff --git a/tests/Provide/Error/ErrorPageTest.php b/tests/Provide/Error/ErrorPageTest.php new file mode 100644 index 00000000..a10383b2 --- /dev/null +++ b/tests/Provide/Error/ErrorPageTest.php @@ -0,0 +1,20 @@ +errorPage = new ErrorPage('some_text_after_error_message'); + } + + public function testToString() + { + $text = (string) $this->errorPage; + $this->assertContains('some_text_after_error_message', $text); + } +} diff --git a/tests/Provide/Error/VndErrorTest.php b/tests/Provide/Error/VndErrorTest.php index ec2ff693..e789b5c0 100644 --- a/tests/Provide/Error/VndErrorTest.php +++ b/tests/Provide/Error/VndErrorTest.php @@ -6,20 +6,19 @@ use BEAR\Package\Provide\Transfer\FakeHttpResponder; use BEAR\Resource\Exception\BadRequestException; use BEAR\Resource\Exception\ResourceNotFoundException; -use BEAR\Resource\Exception\ServerErrorException; use BEAR\Sunday\Extension\Router\RouterMatch; class VndErrorTest extends \PHPUnit_Framework_TestCase { /** - * @var VndError + * @var VndErrorHandler */ private $vndError; public function setUp() { FakeHttpResponder::reset(); - $this->vndError = new VndError(new AppMeta('FakeVendor\HelloWorld'), new FakeHttpResponder()); + $this->vndError = new VndErrorHandler(new AppMeta('FakeVendor\HelloWorld'), new FakeHttpResponder()); } public function testNotFound() @@ -37,13 +36,6 @@ public function testBadRequest() $this->assertSame(400, FakeHttpResponder::$code); } - public function testServerError() - { - $e = new ServerErrorException('message', 501); - $this->vndError->handle($e, new RouterMatch)->transfer(); - $this->assertSame(501, FakeHttpResponder::$code); - } - public function testServerErrorNot50X() { $e = new \RuntimeException('message', 0);