Skip to content

Commit

Permalink
Merge branch 'php7error'
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Mar 25, 2017
2 parents 9357705 + 86ffc09 commit 0f48b45
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
Quit::disable();

$serverRequest = new ServerRequest([], [], '/error-preview', 'GET');
$serverRequest = $serverRequest->withHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
$serverRequest = $serverRequest->withHeader('X-Requested-With', 'XMLHttpRequest');

ob_start();
$closure = function () use ($serverRequest) {
Expand All @@ -99,39 +99,9 @@
}
json
);

});

});

describe('/error-preview/error', function() {

it('show error page', function() {

Quit::disable();

$serverRequest = new ServerRequest([], [], '/error-preview/error', 'GET');
$serverRequest = $serverRequest->withHeader('X_REQUESTED_WITH', 'XMLHttpRequest');

ob_start();
$closure = function () use ($serverRequest) {
$this->app->run($serverRequest);
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$content = ob_get_clean();

expect($content)->toBe(<<<json
{
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Internal Server Error",
"status": 500,
"detail": "We have encountered a problem and we can not fulfill your request. An error report has been generated and send to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience."
}
json
);


});

});

});
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@
expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');

});

});

describe('/error-preview/php7error', function() {

it('show error page', function() {

Quit::disable();

$serverRequest = new ServerRequest([], [], '/error-preview/php7error', 'GET');

ob_start();
$closure = function () use ($serverRequest) {
$this->app->run($serverRequest);
};
expect($closure)->toThrow(new QuitException('Exit statement occurred', -1));
$content = ob_get_clean();

expect($content)->toContain('<p>We have encountered a problem and we can not fulfill your request');

});

});

});
11 changes: 5 additions & 6 deletions spec/Middleware/ExpressiveSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use ErrorHeroModule\Handler\Logging;
use ErrorHeroModule\Middleware\Expressive;
use Kahlan\Plugin\Double;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Console\Console;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Http\PhpEnvironment\Request;

Expand Down Expand Up @@ -88,8 +87,8 @@

$config['enable'] = false;

$request = Double::instance(['implements' => ServerRequestInterface::class]);
$response = Double::instance(['implements' => ResponseInterface::class]);
$request = new ServerRequest();
$response = new Response();
$next = function ($request, $response) {
return new Response();
};
Expand All @@ -102,8 +101,8 @@

it('returns next() when no error', function () {

$request = Double::instance(['implements' => ServerRequestInterface::class]);
$response = Double::instance(['implements' => ResponseInterface::class]);
$request = new ServerRequest();
$response = new Response();
$next = function ($request, $response) {
return new Response();
};
Expand Down
18 changes: 18 additions & 0 deletions spec/Middleware/Routed/Preview/ErrorPreviewActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@

});

it('PHP7 Error', function() {

skipIf(PHP_MAJOR_VERSION < 7);

try {
$request = Double::instance(['implements' => ServerRequestInterface::class]);
allow($request)->toReceive('getAttribute')->with('action', 'exception')->andReturn('php7error');

$response = Double::instance(['implements' => ResponseInterface::class]);
$next = function ($request, $response) {};

$this->middleware->__invoke($request, $response, $next);
} catch (\Throwable $error) {
expect($error)->toBeAnInstanceOf(\Throwable::class);
}

});

});

});
26 changes: 13 additions & 13 deletions src/Handler/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Error;
use ErrorException;
use Exception;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Zend\Diactoros\ServerRequest;
use Zend\Http\PhpEnvironment\Request as HttpRequest;
use Zend\Log\Logger;
use Zend\Log\Writer\Db;
Expand All @@ -28,7 +28,7 @@ class Logging
private $serverUrl;

/**
* @var RequestInterface|ServerRequest
* @var RequestInterface|ServerRequestInterface
*/
private $request;

Expand Down Expand Up @@ -68,14 +68,14 @@ class Logging
private $emailSender;

/**
* @param Logger $logger
* @param string $serverUrl
* @param string $requestUri
* @param RequestInterface|ServerRequest $request
* @param array $errorHeroModuleLocalConfig
* @param array $logWritersConfig
* @param Message|null $mailMessageService
* @param TransportInterface|null $mailMessageTransport
* @param Logger $logger
* @param string $serverUrl
* @param string $requestUri
* @param RequestInterface|ServerRequestInterface $request
* @param array $errorHeroModuleLocalConfig
* @param array $logWritersConfig
* @param Message|null $mailMessageService
* @param TransportInterface|null $mailMessageTransport
*/
public function __construct(
Logger $logger,
Expand All @@ -102,9 +102,9 @@ public function __construct(
/**
* Set ServerRequest for expressive
*
* @param ServerRequest $request
* @param ServerRequestInterface $request
*/
public function setServerRequestandRequestUri(ServerRequest $request)
public function setServerRequestandRequestUri(ServerRequestInterface $request)
{
$this->request = $request;
$this->requestUri = $request->getUri()->getPath();
Expand Down Expand Up @@ -167,7 +167,7 @@ private function getRequestData()
];
}

if ($this->request instanceof ServerRequest) {
if ($this->request instanceof ServerRequestInterface) {
$query = $this->request->getQueryParams();
$request_method = $this->request->getMethod();
$body_data = ($this->request->getMethod() === 'POST')
Expand Down
2 changes: 1 addition & 1 deletion src/HeroTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function phpErrorHandler($errorType, $errorMessage, $errorFile, $errorLin
}

if (! $errorExcluded) {
$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($this->request);
$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled();
}
}
}
3 changes: 1 addition & 2 deletions src/Listener/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ public function exceptionError(Event $e)
/**
* It show default view if display_errors setting = 0.
*
* @param mixed|null $request
*
* @return void
*/
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled($request = null)
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled()
{
$displayErrors = $this->errorHeroModuleConfig['display-settings']['display_errors'];
if ($displayErrors) {
Expand Down
23 changes: 13 additions & 10 deletions src/Middleware/Expressive.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,28 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
}

try {
$response = $next($request, $response);
$this->phpError($request);
$this->request = $request;
$this->logging->setServerRequestandRequestUri($request);

$this->phpError();

$response = $next($request, $response);


return $response;
} catch (Error $e) {
$this->exceptionError($e, $request);
} catch (Exception $e) {
$this->exceptionError($e, $request);
}
}

/**
* @param ServerRequestInterface $request
*
* @return void
*/
public function phpError(ServerRequestInterface $request)
public function phpError()
{
$this->request = $request;

register_shutdown_function([$this, 'execOnShutdown']);
set_error_handler([$this, 'phpErrorHandler']);
}
Expand All @@ -71,20 +75,19 @@ public function phpError(ServerRequestInterface $request)
*/
public function exceptionError($e, $request)
{
$this->logging->setServerRequestandRequestUri($request);
$this->logging->handleException(
$e
);

$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($request);
$this->showDefaultViewWhenDisplayErrorSetttingIsDisabled();
}

/**
* It show default view if display_errors setting = 0.
*
* @return void
*/
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled($request)
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled()
{
$displayErrors = $this->errorHeroModuleConfig['display-settings']['display_errors'];
if ($displayErrors) {
Expand All @@ -94,7 +97,7 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled($request)
$response = new Response();
$response = $response->withStatus(500);

$isXmlHttpRequest = $request->hasHeader('X_REQUESTED_WITH');
$isXmlHttpRequest = $this->request->hasHeader('X-Requested-With');

if ($isXmlHttpRequest === true &&
isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
Expand Down
4 changes: 4 additions & 0 deletions src/Middleware/Routed/Preview/ErrorPreviewAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
throw new \Exception('a sample error preview');
}

if ($action === 'php7error' && class_exists(\Error::class)) {
throw new \Error('error of php 7');
}

$array = [];
$array[1]; // E_NOTICE
}
Expand Down

0 comments on commit 0f48b45

Please sign in to comment.