Skip to content

Commit 39b7aa2

Browse files
committed
Merge branch 'ajax'
* ajax: using use Zend\Http\PhpEnvironment\Request; Request object definition add ajax key in readme add response message definition for XmlHttpRequest request
2 parents 49fd40c + cd97d30 commit 39b7aa2

File tree

6 files changed

+259
-8
lines changed

6 files changed

+259
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Features
1919
- [x] Support excludes [PHP E_* Error](http://www.php.net/manual/en/errorfunc.constants.php) (eg: exclude E_USER_DEPRECATED) in config settings
2020
- [x] Handle only once log error for same error per configured time range
2121
- [x] Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0
22+
- [x] Set default content when request is XMLHttpRequest via 'ajax' configuration.
2223
- [x] Request Information ( http method, raw data, query data, files data )
2324
- [x] Send Mail to listed configured email.
2425

@@ -166,6 +167,15 @@ return [
166167
'console' => [
167168
'message' => '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.',
168169
],
170+
// if enable, display_errors = 0, and request XMLHttpRequest
171+
// on this case, the "template" key will be ignored.
172+
'ajax' => [
173+
'message' => <<<json
174+
{
175+
"error": "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."
176+
}
177+
json
178+
],
169179
],
170180

171181
'logging-settings' => [

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ return [
4949
'view' => 'error-hero-module/error-default'
5050
],
5151

52-
// if enable and display_errors = 0, the console will bring message
52+
// if enable and display_errors = 0, and on console env, the console will bring message
5353
'console' => [
5454
'message' => '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.',
5555
],
5656

57+
// if enable, display_errors = 0, and request XMLHttpRequest
58+
// on this case, the "template" key will be ignored.
59+
'ajax' => [
60+
'message' => <<<json
61+
{
62+
"error": "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."
63+
}
64+
json
65+
],
66+
5767
],
5868
'logging-settings' => [
5969
'same-error-log-time-range' => 86400,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
return [
4+
5+
'db' => [
6+
'username' => 'root',
7+
'password' => '',
8+
'driver' => 'Pdo',
9+
'dsn' => 'mysql:dbname=errorheromodule;host=127.0.0.1',
10+
'driver_options' => [
11+
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
12+
],
13+
],
14+
15+
'log' => [
16+
'ErrorHeroModuleLogger' => [
17+
'writers' => [
18+
19+
[
20+
'name' => 'db',
21+
'options' => [
22+
'db' => 'Zend\Db\Adapter\Adapter',
23+
'table' => 'log',
24+
'column' => [
25+
'timestamp' => 'date',
26+
'priority' => 'type',
27+
'message' => 'event',
28+
'extra' => [
29+
'url' => 'url',
30+
'file' => 'file',
31+
'line' => 'line',
32+
'error_type' => 'error_type',
33+
'trace' => 'trace',
34+
'request_data' => 'request_data',
35+
],
36+
],
37+
],
38+
],
39+
40+
],
41+
],
42+
],
43+
44+
'error-hero-module' => [
45+
'enable' => true,
46+
'display-settings' => [
47+
48+
// excluded php errors
49+
'exclude-php-errors' => [
50+
E_USER_DEPRECATED
51+
],
52+
53+
// show or not error
54+
'display_errors' => 0,
55+
56+
// if enable and display_errors = 0, the page will bring layout and view
57+
'template' => [
58+
'layout' => 'layout/layout',
59+
'view' => 'error-hero-module/error-default'
60+
],
61+
62+
// if enable and display_errors = 0, the console will bring message
63+
'console' => [
64+
'message' => '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.',
65+
],
66+
67+
'ajax' => [
68+
'message' => <<<json
69+
{
70+
"error": "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."
71+
}
72+
json
73+
],
74+
75+
],
76+
'logging-settings' => [
77+
'same-error-log-time-range' => 86400,
78+
],
79+
'email-notification-settings' => [
80+
// set to true to activate email notification on log error
81+
'enable' => false,
82+
83+
// Zend\Mail\Message instance registered at service manager
84+
'mail-message' => 'YourMailMessageService',
85+
86+
// Zend\Mail\Transport\TransportInterface instance registered at service manager
87+
'mail-transport' => 'YourMailTransportService',
88+
89+
// email sender
90+
'email-from' => 'Sender Name <[email protected]>',
91+
92+
'email-to-send' => [
93+
94+
95+
],
96+
],
97+
],
98+
];
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace ErrorHeroModule;
4+
5+
use Zend\Log;
6+
use Zend\ServiceManager\Factory\InvokableFactory;
7+
8+
return [
9+
10+
'controllers' => [
11+
'factories' => [
12+
Controller\ErrorPreviewController::class => InvokableFactory::class,
13+
],
14+
],
15+
16+
'router' => [
17+
'routes' => [
18+
'error-preview' => [
19+
'type' => 'Segment',
20+
'options' => [
21+
'route' => '/error-preview[/][:action]',
22+
'defaults' => [
23+
'controller' => Controller\ErrorPreviewController::class,
24+
'action' => 'exception',
25+
],
26+
],
27+
],
28+
],
29+
],
30+
31+
'service_manager' => [
32+
'abstract_factories' => [
33+
Log\LoggerAbstractServiceFactory::class,
34+
],
35+
'factories' => [
36+
Listener\Mvc::class => Listener\MvcFactory::class,
37+
Handler\Logging::class => Handler\LoggingFactory::class,
38+
],
39+
],
40+
41+
'view_manager' => [
42+
'template_path_stack' => [
43+
__DIR__.'/../view',
44+
],
45+
],
46+
47+
];
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace ErrorHeroModule\Spec\Integration;
4+
5+
use ErrorHeroModule;
6+
use ErrorHeroModule\Controller\ErrorPreviewController;
7+
use Kahlan\Plugin\Quit;
8+
use Kahlan\QuitException;
9+
use Zend\Console\Console;
10+
use Zend\Http\PhpEnvironment\Request;
11+
use Zend\Mvc\Application;
12+
13+
describe('Integration via ErrorPreviewController for XmlHttpRequest', function () {
14+
15+
given('application', function () {
16+
17+
Console::overrideIsConsole(false);
18+
19+
$application = Application::init([
20+
'modules' => [
21+
'Zend\Router',
22+
'Zend\Db',
23+
'ErrorHeroModule',
24+
],
25+
'module_listener_options' => [
26+
'config_glob_paths' => [
27+
realpath(__DIR__).'/Fixture/autoload-for-xmlhttprequest/{{,*.}global,{,*.}local}.php',
28+
],
29+
],
30+
]);
31+
32+
$events = $application->getEventManager();
33+
$serviceManager = $application->getServiceManager();
34+
$serviceManager->get('SendResponseListener')
35+
->detach($events);
36+
37+
return $application;
38+
39+
});
40+
41+
describe('/error-preview', function() {
42+
43+
it('show error page', function() {
44+
45+
skipIf(PHP_MAJOR_VERSION < 7);
46+
Quit::disable();
47+
48+
$request = $this->application->getRequest();
49+
$request->setMethod('GET');
50+
$request->setUri('/error-preview');
51+
52+
allow(Request::class)->toReceive('isXmlHttpRequest')->andReturn(true);
53+
54+
ob_start();
55+
$closure = function () {
56+
$this->application->run();
57+
};
58+
expect($closure)->toThrow(new QuitException());
59+
$content = ob_get_clean();
60+
61+
expect($content)->toBe(<<<json
62+
{
63+
"error": "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."
64+
}
65+
json
66+
);
67+
68+
});
69+
70+
});
71+
72+
});

src/Listener/Mvc.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Zend\Console\Console;
77
use Zend\EventManager\AbstractListenerAggregate;
88
use Zend\EventManager\EventManagerInterface;
9+
use Zend\Http\PhpEnvironment\Request;
910
use Zend\Mvc\MvcEvent;
1011
use Zend\Text\Table;
1112
use Zend\View\Model\ViewModel;
@@ -148,14 +149,27 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled()
148149

149150
if ($displayErrors === 0) {
150151
if (!Console::isConsole()) {
151-
$view = new ViewModel();
152-
$view->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['view']);
153152

154-
$layout = new ViewModel();
155-
$layout->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['layout']);
156-
$layout->setVariable('content', $this->renderer->render($view));
157-
158-
echo $this->renderer->render($layout);
153+
$request = new Request();
154+
$isXmlHttpRequest = $request->isXmlHttpRequest();
155+
if ($isXmlHttpRequest === true &&
156+
isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
157+
) {
158+
echo $this->errorHeroModuleConfig['display-settings']['ajax']['message'];
159+
}
160+
161+
if ($isXmlHttpRequest === false ||
162+
! isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
163+
) {
164+
$view = new ViewModel();
165+
$view->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['view']);
166+
167+
$layout = new ViewModel();
168+
$layout->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['layout']);
169+
$layout->setVariable('content', $this->renderer->render($view));
170+
171+
echo $this->renderer->render($layout);
172+
}
159173
} else {
160174
$table = new Table\Table([
161175
'columnWidths' => [150],

0 commit comments

Comments
 (0)