From 017b8a680d4f005a211430f3dc77df545e8cdd04 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 26 Mar 2017 04:18:30 +0700 Subject: [PATCH 1/3] catch php7 error --- ...wActionMiddlewareForXmlHttpRequestSpec.php | 34 ++----------------- ...ionViaErrorPreviewActionMiddlewareSpec.php | 22 ++++++++++++ spec/Middleware/ExpressiveSpec.php | 10 +++--- .../Routed/Preview/ErrorPreviewActionSpec.php | 18 ++++++++++ src/Handler/Logging.php | 26 +++++++------- src/HeroTrait.php | 2 +- src/Listener/Mvc.php | 3 +- src/Middleware/Expressive.php | 23 +++++++------ .../Routed/Preview/ErrorPreviewAction.php | 4 +++ 9 files changed, 79 insertions(+), 63 deletions(-) diff --git a/spec/Integration/IntegrationViaErrorPreviewActionMiddlewareForXmlHttpRequestSpec.php b/spec/Integration/IntegrationViaErrorPreviewActionMiddlewareForXmlHttpRequestSpec.php index ce98dc29..264b3c8a 100644 --- a/spec/Integration/IntegrationViaErrorPreviewActionMiddlewareForXmlHttpRequestSpec.php +++ b/spec/Integration/IntegrationViaErrorPreviewActionMiddlewareForXmlHttpRequestSpec.php @@ -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) { @@ -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(<<toContain('

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('

We have encountered a problem and we can not fulfill your request'); + + }); + }); }); diff --git a/spec/Middleware/ExpressiveSpec.php b/spec/Middleware/ExpressiveSpec.php index 92a339b6..59b190d8 100644 --- a/spec/Middleware/ExpressiveSpec.php +++ b/spec/Middleware/ExpressiveSpec.php @@ -6,9 +6,9 @@ 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; @@ -88,8 +88,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(); }; @@ -102,8 +102,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(); }; diff --git a/spec/Middleware/Routed/Preview/ErrorPreviewActionSpec.php b/spec/Middleware/Routed/Preview/ErrorPreviewActionSpec.php index f01f6948..f5a656c9 100644 --- a/spec/Middleware/Routed/Preview/ErrorPreviewActionSpec.php +++ b/spec/Middleware/Routed/Preview/ErrorPreviewActionSpec.php @@ -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); + } + + }); + }); }); diff --git a/src/Handler/Logging.php b/src/Handler/Logging.php index c69decc6..cd311464 100644 --- a/src/Handler/Logging.php +++ b/src/Handler/Logging.php @@ -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; @@ -28,7 +28,7 @@ class Logging private $serverUrl; /** - * @var RequestInterface|ServerRequest + * @var RequestInterface|ServerRequestInterface */ private $request; @@ -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, @@ -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(); @@ -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') diff --git a/src/HeroTrait.php b/src/HeroTrait.php index e57ceddc..cc19483d 100644 --- a/src/HeroTrait.php +++ b/src/HeroTrait.php @@ -89,7 +89,7 @@ public function phpErrorHandler($errorType, $errorMessage, $errorFile, $errorLin } if (! $errorExcluded) { - $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($this->request); + $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled(); } } } diff --git a/src/Listener/Mvc.php b/src/Listener/Mvc.php index afff307f..dd670ad1 100644 --- a/src/Listener/Mvc.php +++ b/src/Listener/Mvc.php @@ -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) { diff --git a/src/Middleware/Expressive.php b/src/Middleware/Expressive.php index ef20b223..21c6d1bd 100644 --- a/src/Middleware/Expressive.php +++ b/src/Middleware/Expressive.php @@ -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']); } @@ -71,12 +75,11 @@ public function phpError(ServerRequestInterface $request) */ public function exceptionError($e, $request) { - $this->logging->setServerRequestandRequestUri($request); $this->logging->handleException( $e ); - $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($request); + $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled(); } /** @@ -84,7 +87,7 @@ public function exceptionError($e, $request) * * @return void */ - private function showDefaultViewWhenDisplayErrorSetttingIsDisabled($request) + private function showDefaultViewWhenDisplayErrorSetttingIsDisabled() { $displayErrors = $this->errorHeroModuleConfig['display-settings']['display_errors']; if ($displayErrors) { @@ -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']) diff --git a/src/Middleware/Routed/Preview/ErrorPreviewAction.php b/src/Middleware/Routed/Preview/ErrorPreviewAction.php index c3f8aef6..5fab0cd0 100644 --- a/src/Middleware/Routed/Preview/ErrorPreviewAction.php +++ b/src/Middleware/Routed/Preview/ErrorPreviewAction.php @@ -15,6 +15,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res throw new \Exception('a sample error preview'); } + if ($action === 'php7error') { + throw new \Error('error of php 7'); + } + $array = []; $array[1]; // E_NOTICE } From ca3e58aaa999f226fd8a4676073feffc72d0c2c4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 26 Mar 2017 04:20:46 +0700 Subject: [PATCH 2/3] remove unused import --- spec/Middleware/ExpressiveSpec.php | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/Middleware/ExpressiveSpec.php b/spec/Middleware/ExpressiveSpec.php index 59b190d8..dec95ff2 100644 --- a/spec/Middleware/ExpressiveSpec.php +++ b/spec/Middleware/ExpressiveSpec.php @@ -5,7 +5,6 @@ use ErrorHeroModule\Handler\Logging; use ErrorHeroModule\Middleware\Expressive; use Kahlan\Plugin\Double; -use Psr\Http\Message\ResponseInterface; use Zend\Console\Console; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; From 86ffc097c8774897f05fe7081f3699827951f55b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 26 Mar 2017 04:32:06 +0700 Subject: [PATCH 3/3] class_exists(\Error::class) check for php5.6 --- src/Middleware/Routed/Preview/ErrorPreviewAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Middleware/Routed/Preview/ErrorPreviewAction.php b/src/Middleware/Routed/Preview/ErrorPreviewAction.php index 5fab0cd0..1bf14b71 100644 --- a/src/Middleware/Routed/Preview/ErrorPreviewAction.php +++ b/src/Middleware/Routed/Preview/ErrorPreviewAction.php @@ -15,7 +15,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res throw new \Exception('a sample error preview'); } - if ($action === 'php7error') { + if ($action === 'php7error' && class_exists(\Error::class)) { throw new \Error('error of php 7'); }