Skip to content

Commit 6473592

Browse files
committed
Merge branch 'specific-error-exception'
* specific-error-exception: test clean up clean up cs cs clean up added ability to exclude specific PHP E_* error and Exception with specific message
2 parents ba88520 + 1a4ebd7 commit 6473592

File tree

10 files changed

+316
-22
lines changed

10 files changed

+316
-22
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Features
2121

2222
- [x] Save to DB with Db Writer Adapter.
2323
- [x] Log Exception (dispatch.error and render.error) and PHP Errors in all events process.
24-
- [x] Support excludes [PHP E_* Error](http://www.php.net/manual/en/errorfunc.constants.php) (eg: exclude E_USER_DEPRECATED) in config settings.
25-
- [x] Support excludes [PHP Exception](http://php.net/manual/en/spl.exceptions.php) (eg: Exception class or classes that extends it) in config settings.
24+
- [x] Support excludes [PHP E_* Error](http://www.php.net/manual/en/errorfunc.constants.php) (eg: exclude E_USER_DEPRECATED or specific E_USER_DEPRECATED with specific message) in config settings.
25+
- [x] Support excludes [PHP Exception](http://php.net/manual/en/spl.exceptions.php) (eg: Exception class or classes that extends it or specific exception class with specific message) in config settings.
2626
- [x] Handle only once log error for same error per configured time range.
2727
- [x] Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0.
2828
- [x] Set default content when request is XMLHttpRequest via 'ajax' configuration.
@@ -200,12 +200,27 @@ return [
200200

201201
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
202202
'exclude-php-errors' => [
203+
204+
// can be specific error
203205
\E_USER_DEPRECATED,
206+
207+
// can be specific error with specific message
208+
[\E_WARNING, 'specific error message'],
209+
204210
],
205211

206212
// excluded exceptions
207213
'exclude-exceptions' => [
208-
\App\Exception\MyException::class, // can be an Exception class or class extends Exception class
214+
215+
// can be an Exception class or class extends Exception class
216+
\App\Exception\MyException::class,
217+
218+
// can be specific exception with specific message
219+
[\RuntimeException::class, 'specific exception message'],
220+
221+
// or specific Error class with specific message
222+
[\Error::class, 'specific error message'],
223+
209224
],
210225

211226
// show or not error

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,29 @@ return [
5353

5454
'display-settings' => [
5555

56-
// excluded php errors
56+
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
5757
'exclude-php-errors' => [
58-
\E_USER_DEPRECATED
58+
59+
// can be specific error
60+
\E_USER_DEPRECATED,
61+
62+
// can be specific error with specific message
63+
[\E_WARNING, 'specific error message'],
64+
5965
],
6066

6167
// excluded exceptions
6268
'exclude-exceptions' => [
63-
\Application\Exception\MyException::class, // can be an Exception class or class extends Exception class
69+
70+
// can be an Exception class or class extends Exception class
71+
\App\Exception\MyException::class,
72+
73+
// can be specific exception with specific message
74+
[\RuntimeException::class, 'specific exception message'],
75+
76+
// or specific Error class with specific message
77+
[\Error::class, 'specific error message'],
78+
6479
],
6580

6681
// show or not error

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,29 @@ return [
4949

5050
'display-settings' => [
5151

52-
// excluded php errors
52+
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
5353
'exclude-php-errors' => [
54-
\E_USER_DEPRECATED
54+
55+
// can be specific error
56+
\E_USER_DEPRECATED,
57+
58+
// can be specific error with specific message
59+
[\E_WARNING, 'specific error message'],
60+
5561
],
5662

5763
// excluded exceptions
5864
'exclude-exceptions' => [
59-
\App\Exception\MyException::class, // can be an Exception class or class extends Exception class
65+
66+
// can be an Exception class or class extends Exception class
67+
\App\Exception\MyException::class,
68+
69+
// can be specific exception with specific message
70+
[\RuntimeException::class, 'specific exception message'],
71+
72+
// or specific Error class with specific message
73+
[\Error::class, 'specific error message'],
74+
6075
],
6176

6277
// show or not error
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
use Zend\Db\Adapter\AdapterInterface;
4+
5+
return [
6+
7+
'db' => [
8+
'username' => 'root',
9+
'password' => '',
10+
'driver' => 'Pdo',
11+
'dsn' => 'mysql:dbname=errorheromodule;host=127.0.0.1',
12+
'driver_options' => [
13+
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
14+
],
15+
],
16+
17+
'log' => [
18+
'ErrorHeroModuleLogger' => [
19+
'writers' => [
20+
21+
[
22+
'name' => 'db',
23+
'options' => [
24+
'db' => AdapterInterface::class,
25+
'table' => 'log',
26+
'column' => [
27+
'timestamp' => 'date',
28+
'priority' => 'type',
29+
'message' => 'event',
30+
'extra' => [
31+
'url' => 'url',
32+
'file' => 'file',
33+
'line' => 'line',
34+
'error_type' => 'error_type',
35+
'trace' => 'trace',
36+
'request_data' => 'request_data',
37+
],
38+
],
39+
],
40+
],
41+
42+
],
43+
],
44+
],
45+
46+
'error-hero-module' => [
47+
'enable' => true,
48+
'display-settings' => [
49+
50+
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
51+
'exclude-php-errors' => [
52+
53+
// can be specific error with specific message
54+
[\E_NOTICE, 'Undefined offset: 1'],
55+
56+
],
57+
58+
// excluded exceptions
59+
'exclude-exceptions' => [
60+
61+
// can be specific exception instance with specific error message
62+
[\Exception::class, 'a sample exception preview'],
63+
64+
// or Error class
65+
[\Error::class, 'a sample error preview'],
66+
],
67+
68+
// show or not error
69+
'display_errors' => 0,
70+
71+
// if enable and display_errors = 0, the page will bring layout and view
72+
'template' => [
73+
'layout' => 'layout/layout',
74+
'view' => 'error-hero-module/error-default'
75+
],
76+
77+
// if enable and display_errors = 0, the console will bring message
78+
'console' => [
79+
'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.',
80+
],
81+
82+
],
83+
'logging-settings' => [
84+
'same-error-log-time-range' => 86400,
85+
],
86+
'email-notification-settings' => [
87+
// set to true to activate email notification on log error
88+
'enable' => false,
89+
90+
// Zend\Mail\Message instance registered at service manager
91+
'mail-message' => 'YourMailMessageService',
92+
93+
// Zend\Mail\Transport\TransportInterface instance registered at service manager
94+
'mail-transport' => 'YourMailTransportService',
95+
96+
// email sender
97+
'email-from' => 'Sender Name <[email protected]>',
98+
99+
'email-to-send' => [
100+
101+
102+
],
103+
],
104+
],
105+
];
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace ErrorHeroModule\Spec;
4+
5+
use ErrorHeroModule;
6+
use Zend\Console\Console;
7+
use Zend\Db\Adapter\AdapterInterface;
8+
use Zend\Db\ResultSet\ResultSet;
9+
use Zend\Db\TableGateway\TableGateway;
10+
use Zend\Mvc\Application;
11+
12+
describe('Integration via ErrorPreviewController', function () {
13+
14+
given('application', function () {
15+
16+
Console::overrideIsConsole(false);
17+
18+
$application = Application::init([
19+
'modules' => [
20+
'Zend\Router',
21+
'Zend\Db',
22+
'ErrorHeroModule',
23+
],
24+
'module_listener_options' => [
25+
'config_glob_paths' => [
26+
\realpath(__DIR__).'/../Fixture/config/autoload-for-specific-error-and-exception/error-hero-module.local.php',
27+
\realpath(__DIR__).'/../Fixture/config/module.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('empty as rely to original mvc process to handle', function() {
44+
45+
$request = $this->application->getRequest();
46+
$request->setMethod('GET');
47+
$request->setUri('http://example.com/error-preview');
48+
$request->setRequestUri('/error-preview');
49+
50+
\ob_start();
51+
$this->application->run();
52+
$content = \ob_get_clean();
53+
54+
expect(\ob_get_clean())->toBe('');
55+
expect($this->application->getResponse()->getStatusCode())->toBe(500);
56+
57+
});
58+
59+
});
60+
61+
describe('/error-preview/notice', function() {
62+
63+
it('empty as rely to original mvc process to handle', function() {
64+
65+
@mkdir(__DIR__ . '/../Fixture/view/error-hero-module/error-preview', 0755, true);
66+
file_put_contents(__DIR__ . '/../Fixture/view/error-hero-module/error-preview/notice.phtml', '');
67+
68+
$request = $this->application->getRequest();
69+
$request->setMethod('GET');
70+
$request->setUri('http://example.com/error-preview/notice');
71+
$request->setRequestUri('/error-preview/notice');
72+
73+
\ob_start();
74+
$this->application->run();
75+
$content = \ob_get_clean();
76+
77+
expect(\ob_get_clean())->toBe('');
78+
// yes, excluded E_* error means it ignored, means it passed, means it is 200 status code
79+
expect($this->application->getResponse()->getStatusCode())->toBe(200);
80+
81+
unlink(__DIR__ . '/../Fixture/view/error-hero-module/error-preview/notice.phtml');
82+
rmdir(__DIR__ . '/../Fixture/view/error-hero-module/error-preview');
83+
rmdir(__DIR__ . '/../Fixture/view/error-hero-module');
84+
85+
});
86+
87+
});
88+
89+
describe('/error-preview/error', function() {
90+
91+
it('empty as rely to original mvc process to handle', function() {
92+
93+
$request = $this->application->getRequest();
94+
$request->setMethod('GET');
95+
$request->setUri('http://example.com/error-preview/error');
96+
$request->setRequestUri('/error-preview/error');
97+
98+
\ob_start();
99+
$this->application->run();
100+
$content = \ob_get_clean();
101+
102+
expect(\ob_get_clean())->toBe('');
103+
expect($this->application->getResponse()->getStatusCode())->toBe(500);
104+
105+
106+
});
107+
108+
});
109+
110+
});

src/Handler/Formatter/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace ErrorHeroModule\Handler\Formatter;
66

77
use DateTime;
8-
use Zend\Log\Formatter\Json as BaseJson;
98
use Zend\Log\Formatter\FormatterInterface;
9+
use Zend\Log\Formatter\Json as BaseJson;
1010

1111
class Json extends BaseJson implements FormatterInterface
1212
{

src/HeroFunction.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,34 @@
55
namespace ErrorHeroModule;
66

77
use Seld\JsonLint\JsonParser;
8+
use Throwable;
89

910
function detectMessageContentType(string $message) : string
1011
{
1112
return (new JsonParser())->lint($message) === null
1213
? 'application/problem+json'
1314
: ((\strip_tags($message) === $message) ? 'text/plain' : 'text/html');
1415
}
16+
17+
function isExcludedException(array $excludeExceptionsConfig, Throwable $t)
18+
{
19+
$exceptionOrErrorClass = \get_class($t);
20+
21+
$isExcluded = false;
22+
foreach ($excludeExceptionsConfig as $excludeException) {
23+
if ($exceptionOrErrorClass === $excludeException) {
24+
$isExcluded = true;
25+
break;
26+
}
27+
28+
if (is_array($excludeException)
29+
&& $excludeException[0] === $exceptionOrErrorClass
30+
&& $excludeException[1] === $t->getMessage()
31+
) {
32+
$isExcluded = true;
33+
break;
34+
}
35+
}
36+
37+
return $isExcluded;
38+
}

src/HeroTrait.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@ public function phpErrorHandler(int $errorType, string $errorMessage, string $er
104104
return;
105105
}
106106

107-
if (\in_array($errorType, $this->errorHeroModuleConfig['display-settings']['exclude-php-errors'])) {
108-
return;
107+
foreach ($this->errorHeroModuleConfig['display-settings']['exclude-php-errors'] as $excludePhpError) {
108+
if ($errorType === $excludePhpError) {
109+
return;
110+
}
111+
112+
if (is_array($excludePhpError)
113+
&& $excludePhpError[0] === $errorType
114+
&& $excludePhpError[1] === $errorMessage
115+
) {
116+
return;
117+
}
109118
}
110119

111120
throw new ErrorException($errorMessage, 0, $errorType, $errorFile, $errorLine);

0 commit comments

Comments
 (0)