diff --git a/README.md b/README.md index c0f4e562..f846ec54 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,19 @@ return [ 'view' => 'error-hero-module/error-default' ], + // for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service + // if enable, and display_errors = 0, then show a message under no_template config + 'no_template' => [ + 'message' => << [ 'message' => 'We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.', diff --git a/config/expressive-error-hero-module.local.php.dist b/config/expressive-error-hero-module.local.php.dist index 5c6dfd2b..d6f8b418 100644 --- a/config/expressive-error-hero-module.local.php.dist +++ b/config/expressive-error-hero-module.local.php.dist @@ -63,6 +63,19 @@ return [ 'view' => 'error-hero-module::error-default' ], + // for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service + // if enable, and display_errors = 0, then show a message under no_template config + 'no_template' => [ + 'message' => << [ 'message' => 'We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.', diff --git a/spec/Middleware/ExpressiveSpec.php b/spec/Middleware/ExpressiveSpec.php index 0c90c669..f45019d6 100644 --- a/spec/Middleware/ExpressiveSpec.php +++ b/spec/Middleware/ExpressiveSpec.php @@ -11,6 +11,7 @@ use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; use Zend\Diactoros\Uri; +use Zend\Expressive\Template\TemplateRendererInterface; use Zend\Expressive\ZendView\ZendViewRenderer; use Zend\Http\PhpEnvironment\Request; use Zend\Log\Logger; @@ -101,6 +102,19 @@ 'view' => 'error-hero-module/error-default' ], + // for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service + // if enable, and display_errors = 0, then show a message under no_template config + 'no_template' => [ + 'message' => << [ 'message' => <<config; + $config['display-settings']['display_errors'] = 0; + + $logging = new Logging( + $this->logger, + $config, + $this->logWritersConfig, + null, + null + ); + + $request = new ServerRequest( + [], + [], + new Uri('http://example.com'), + 'GET', + 'php://memory', + [], + [], + [], + '', + '1.2' + ); + $request = $request->withHeader('X-Requested-With', 'XmlHttpRequest'); + $handler = Double::instance(['implements' => RequestHandlerInterface::class]); + allow($handler)->toReceive('handle')->with($request)->andRun(function () { + throw new \Exception('message'); + }); + $middleware = new Expressive($config, $logging, null); + + $actual = $middleware->process($request, $handler); + expect($actual)->toBeAnInstanceOf(Response::class); + expect($actual->getBody()->__toString())->toBe(<<config; diff --git a/src/Middleware/Expressive.php b/src/Middleware/Expressive.php index 6f03643d..0b1c29f0 100644 --- a/src/Middleware/Expressive.php +++ b/src/Middleware/Expressive.php @@ -28,14 +28,14 @@ class Expressive implements MiddlewareInterface use HeroTrait; /** - * @var TemplateRendererInterface + * @var TemplateRendererInterface|null */ private $renderer; public function __construct( array $errorHeroModuleConfig, Logging $logging, - TemplateRendererInterface $renderer + TemplateRendererInterface $renderer = null ) { $this->errorHeroModuleConfig = $errorHeroModuleConfig; $this->logging = $logging; @@ -87,24 +87,32 @@ public function exceptionError(Throwable $t, ServerRequestInterface $request) : return $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($request); } + private function responseByConfigMessage($key) : ResponseInterface + { + $message = $this->errorHeroModuleConfig['display-settings'][$key]['message']; + $contentType = detectAjaxMessageContentType($message); + + $response = new Response(); + $response->getBody()->write($message); + $response = $response->withHeader('Content-type', $contentType); + + return $response->withStatus(500); + } + /** * It show default view if display_errors setting = 0. */ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(ServerRequestInterface $request) : ResponseInterface { - $isXmlHttpRequest = $request->hasHeader('X-Requested-With'); + if ($this->renderer === null) { + return $this->responseByConfigMessage('no_template'); + } + $isXmlHttpRequest = $request->hasHeader('X-Requested-With'); if ($isXmlHttpRequest === true && isset($this->errorHeroModuleConfig['display-settings']['ajax']['message']) ) { - $message = $this->errorHeroModuleConfig['display-settings']['ajax']['message']; - $contentType = detectAjaxMessageContentType($message); - - $response = new Response(); - $response->getBody()->write($message); - $response = $response->withHeader('Content-type', $contentType); - - return $response->withStatus(500); + return $this->responseByConfigMessage('ajax'); } if ($this->renderer instanceof ZendViewRenderer) { diff --git a/src/Middleware/ExpressiveFactory.php b/src/Middleware/ExpressiveFactory.php index bd931be6..897b8d01 100644 --- a/src/Middleware/ExpressiveFactory.php +++ b/src/Middleware/ExpressiveFactory.php @@ -34,7 +34,9 @@ private function createMiddlewareInstance(ContainerInterface $container, array $ return new Expressive( $configuration['error-hero-module'], $container->get(Logging::class), - $container->get(TemplateRendererInterface::class) + $container->has(TemplateRendererInterface::class) + ? $container->get(TemplateRendererInterface::class) + : null ); }