Skip to content

Commit 26329f5

Browse files
authored
Merge pull request #47 from samsonasik/no-template-expressive
add no_template config to set default error message on Expressive 3
2 parents 43c2e6f + fea565b commit 26329f5

File tree

5 files changed

+108
-12
lines changed

5 files changed

+108
-12
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ return [
209209
'view' => 'error-hero-module/error-default'
210210
],
211211

212+
// for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service
213+
// if enable, and display_errors = 0, then show a message under no_template config
214+
'no_template' => [
215+
'message' => <<<json
216+
{
217+
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
218+
"title": "Internal Server Error",
219+
"status": 500,
220+
"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."
221+
}
222+
json
223+
],
224+
212225
// if enable and display_errors = 0, the console will bring message
213226
'console' => [
214227
'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.',

config/expressive-error-hero-module.local.php.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ return [
6363
'view' => 'error-hero-module::error-default'
6464
],
6565

66+
// for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service
67+
// if enable, and display_errors = 0, then show a message under no_template config
68+
'no_template' => [
69+
'message' => <<<json
70+
{
71+
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
72+
"title": "Internal Server Error",
73+
"status": 500,
74+
"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."
75+
}
76+
json
77+
],
78+
6679
// if enable and display_errors = 0, and on console env, the console will bring message
6780
'console' => [
6881
'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.',

spec/Middleware/ExpressiveSpec.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Zend\Diactoros\Response;
1212
use Zend\Diactoros\ServerRequest;
1313
use Zend\Diactoros\Uri;
14+
use Zend\Expressive\Template\TemplateRendererInterface;
1415
use Zend\Expressive\ZendView\ZendViewRenderer;
1516
use Zend\Http\PhpEnvironment\Request;
1617
use Zend\Log\Logger;
@@ -101,6 +102,19 @@
101102
'view' => 'error-hero-module/error-default'
102103
],
103104

105+
// for expressive, when container doesn't has \Zend\Expressive\Template\TemplateRendererInterface service
106+
// if enable, and display_errors = 0, then show a message under no_template config
107+
'no_template' => [
108+
'message' => <<<json
109+
{
110+
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
111+
"title": "Internal Server Error",
112+
"status": 500,
113+
"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."
114+
}
115+
json
116+
],
117+
104118
'ajax' => [
105119
'message' => <<<json
106120
{
@@ -332,6 +346,52 @@
332346

333347
});
334348

349+
it('passed renderer is null returns error message on display_errors = 0', function () {
350+
351+
$config = $this->config;
352+
$config['display-settings']['display_errors'] = 0;
353+
354+
$logging = new Logging(
355+
$this->logger,
356+
$config,
357+
$this->logWritersConfig,
358+
null,
359+
null
360+
);
361+
362+
$request = new ServerRequest(
363+
[],
364+
[],
365+
new Uri('http://example.com'),
366+
'GET',
367+
'php://memory',
368+
[],
369+
[],
370+
[],
371+
'',
372+
'1.2'
373+
);
374+
$request = $request->withHeader('X-Requested-With', 'XmlHttpRequest');
375+
$handler = Double::instance(['implements' => RequestHandlerInterface::class]);
376+
allow($handler)->toReceive('handle')->with($request)->andRun(function () {
377+
throw new \Exception('message');
378+
});
379+
$middleware = new Expressive($config, $logging, null);
380+
381+
$actual = $middleware->process($request, $handler);
382+
expect($actual)->toBeAnInstanceOf(Response::class);
383+
expect($actual->getBody()->__toString())->toBe(<<<json
384+
{
385+
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
386+
"title": "Internal Server Error",
387+
"status": 500,
388+
"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."
389+
}
390+
json
391+
);
392+
393+
});
394+
335395
it('xmlhttprequest: returns error page on display_errors = 0', function () {
336396

337397
$config = $this->config;

src/Middleware/Expressive.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class Expressive implements MiddlewareInterface
2828
use HeroTrait;
2929

3030
/**
31-
* @var TemplateRendererInterface
31+
* @var TemplateRendererInterface|null
3232
*/
3333
private $renderer;
3434

3535
public function __construct(
3636
array $errorHeroModuleConfig,
3737
Logging $logging,
38-
TemplateRendererInterface $renderer
38+
TemplateRendererInterface $renderer = null
3939
) {
4040
$this->errorHeroModuleConfig = $errorHeroModuleConfig;
4141
$this->logging = $logging;
@@ -87,24 +87,32 @@ public function exceptionError(Throwable $t, ServerRequestInterface $request) :
8787
return $this->showDefaultViewWhenDisplayErrorSetttingIsDisabled($request);
8888
}
8989

90+
private function responseByConfigMessage($key) : ResponseInterface
91+
{
92+
$message = $this->errorHeroModuleConfig['display-settings'][$key]['message'];
93+
$contentType = detectAjaxMessageContentType($message);
94+
95+
$response = new Response();
96+
$response->getBody()->write($message);
97+
$response = $response->withHeader('Content-type', $contentType);
98+
99+
return $response->withStatus(500);
100+
}
101+
90102
/**
91103
* It show default view if display_errors setting = 0.
92104
*/
93105
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(ServerRequestInterface $request) : ResponseInterface
94106
{
95-
$isXmlHttpRequest = $request->hasHeader('X-Requested-With');
107+
if ($this->renderer === null) {
108+
return $this->responseByConfigMessage('no_template');
109+
}
96110

111+
$isXmlHttpRequest = $request->hasHeader('X-Requested-With');
97112
if ($isXmlHttpRequest === true &&
98113
isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
99114
) {
100-
$message = $this->errorHeroModuleConfig['display-settings']['ajax']['message'];
101-
$contentType = detectAjaxMessageContentType($message);
102-
103-
$response = new Response();
104-
$response->getBody()->write($message);
105-
$response = $response->withHeader('Content-type', $contentType);
106-
107-
return $response->withStatus(500);
115+
return $this->responseByConfigMessage('ajax');
108116
}
109117

110118
if ($this->renderer instanceof ZendViewRenderer) {

src/Middleware/ExpressiveFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ private function createMiddlewareInstance(ContainerInterface $container, array $
3434
return new Expressive(
3535
$configuration['error-hero-module'],
3636
$container->get(Logging::class),
37-
$container->get(TemplateRendererInterface::class)
37+
$container->has(TemplateRendererInterface::class)
38+
? $container->get(TemplateRendererInterface::class)
39+
: null
3840
);
3941
}
4042

0 commit comments

Comments
 (0)