Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/vnd-error' into 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Sep 19, 2015
2 parents 2f2c1bf + fdc03c4 commit 80dcc4b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Provide/Error/ExceptionAsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
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;

/**
* vnd.error for BEAR.Package
*
* @see https://github.com/blongden/vnd.error
*/
class VndError implements ErrorInterface
class VndErrorHandler implements ErrorInterface
{
/**
* @var int
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -123,7 +134,7 @@ private function codeError(\Exception $e)

/**
* @param \Exception $e
* @param Request $request
* @param Request $request
*
* @return int logRef
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Provide/Error/VndErrorModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions tests/Provide/Error/ErrorPageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace BEAR\Package\Provide\Error;

class ErrorPageTest extends \PHPUnit_Framework_TestCase
{
private $errorPage;

public function setUp()
{
parent::setUp();
$this->errorPage = new ErrorPage('some_text_after_error_message');
}

public function testToString()
{
$text = (string) $this->errorPage;
$this->assertContains('some_text_after_error_message', $text);
}
}
12 changes: 2 additions & 10 deletions tests/Provide/Error/VndErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down

0 comments on commit 80dcc4b

Please sign in to comment.