Skip to content

Commit

Permalink
add no_template config to set default error message on Expressive 3 a…
Browse files Browse the repository at this point in the history
…pplication when no TemplateRendererInterface service
  • Loading branch information
samsonasik committed Jun 5, 2018
1 parent 43c2e6f commit fea565b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -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' => <<<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 sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience."
}
json
],

// if enable and display_errors = 0, the console will bring message
'console' => [
'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.',
Expand Down
13 changes: 13 additions & 0 deletions config/expressive-error-hero-module.local.php.dist
Expand Up @@ -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' => <<<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 sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience."
}
json
],

// if enable and display_errors = 0, and on console env, the console will bring message
'console' => [
'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.',
Expand Down
60 changes: 60 additions & 0 deletions spec/Middleware/ExpressiveSpec.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => <<<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 sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience."
}
json
],

'ajax' => [
'message' => <<<json
{
Expand Down Expand Up @@ -332,6 +346,52 @@

});

it('passed renderer is null returns error message on display_errors = 0', function () {

$config = $this->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(<<<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 sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience."
}
json
);

});

it('xmlhttprequest: returns error page on display_errors = 0', function () {

$config = $this->config;
Expand Down
30 changes: 19 additions & 11 deletions src/Middleware/Expressive.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/Middleware/ExpressiveFactory.php
Expand Up @@ -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
);
}

Expand Down

0 comments on commit fea565b

Please sign in to comment.