Skip to content

Commit

Permalink
Merge d3d18d2 into ba88520
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 14, 2019
2 parents ba88520 + d3d18d2 commit 358414a
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 23 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Features

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

// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
'exclude-php-errors' => [

// can be specific error
\E_USER_DEPRECATED,

// can be specific error with specific message
[\E_WARNING, 'specific error message'],

],

// excluded exceptions
'exclude-exceptions' => [
\App\Exception\MyException::class, // can be an Exception class or class extends Exception class

// can be an Exception class or class extends Exception class
\App\Exception\MyException::class,

// can be specific exception with specific message
[\RuntimeException::class, 'specific exception message'],

// or specific Error class with specific message
[\Error::class, 'specific error message'],

],

// show or not error
Expand Down
21 changes: 18 additions & 3 deletions config/error-hero-module.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,29 @@ return [

'display-settings' => [

// excluded php errors
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
'exclude-php-errors' => [
\E_USER_DEPRECATED

// can be specific error
\E_USER_DEPRECATED,

// can be specific error with specific message
[\E_WARNING, 'specific error message'],

],

// excluded exceptions
'exclude-exceptions' => [
\Application\Exception\MyException::class, // can be an Exception class or class extends Exception class

// can be an Exception class or class extends Exception class
\App\Exception\MyException::class,

// can be specific exception with specific message
[\RuntimeException::class, 'specific exception message'],

// or specific Error class with specific message
[\Error::class, 'specific error message'],

],

// show or not error
Expand Down
21 changes: 18 additions & 3 deletions config/expressive-error-hero-module.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,29 @@ return [

'display-settings' => [

// excluded php errors
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
'exclude-php-errors' => [
\E_USER_DEPRECATED

// can be specific error
\E_USER_DEPRECATED,

// can be specific error with specific message
[\E_WARNING, 'specific error message'],

],

// excluded exceptions
'exclude-exceptions' => [
\App\Exception\MyException::class, // can be an Exception class or class extends Exception class

// can be an Exception class or class extends Exception class
\App\Exception\MyException::class,

// can be specific exception with specific message
[\RuntimeException::class, 'specific exception message'],

// or specific Error class with specific message
[\Error::class, 'specific error message'],

],

// show or not error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

use Zend\Db\Adapter\AdapterInterface;

return [

'db' => [
'username' => 'root',
'password' => '',
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=errorheromodule;host=127.0.0.1',
'driver_options' => [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
],
],

'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [

[
'name' => 'db',
'options' => [
'db' => AdapterInterface::class,
'table' => 'log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data',
],
],
],
],

],
],
],

'error-hero-module' => [
'enable' => true,
'display-settings' => [

// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
'exclude-php-errors' => [

// can be specific error with specific message
[\E_NOTICE, 'Undefined offset: 1'],

],

// excluded exceptions
'exclude-exceptions' => [

// can be specific exception instance with specific error message
[\Exception::class, 'a sample exception preview'],

// or Error class
[\Error::class, 'a sample error preview'],
],

// show or not error
'display_errors' => 0,

// if enable and display_errors = 0, the page will bring layout and view
'template' => [
'layout' => 'layout/layout',
'view' => 'error-hero-module/error-default'
],

// 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.',
],

],
'logging-settings' => [
'same-error-log-time-range' => 86400,
],
'email-notification-settings' => [
// set to true to activate email notification on log error
'enable' => false,

// Zend\Mail\Message instance registered at service manager
'mail-message' => 'YourMailMessageService',

// Zend\Mail\Transport\TransportInterface instance registered at service manager
'mail-transport' => 'YourMailTransportService',

// email sender
'email-from' => 'Sender Name <sender@host.com>',

'email-to-send' => [
'developer1@foo.com',
'developer2@foo.com',
],
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace ErrorHeroModule\Spec;

use ErrorHeroModule;
use Zend\Console\Console;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\Application;

describe('Integration via ErrorPreviewController', function () {

given('application', function () {

Console::overrideIsConsole(false);

$application = Application::init([
'modules' => [
'Zend\Router',
'Zend\Db',
'ErrorHeroModule',
],
'module_listener_options' => [
'config_glob_paths' => [
\realpath(__DIR__).'/../Fixture/config/autoload-for-specific-error-and-exception/error-hero-module.local.php',
\realpath(__DIR__).'/../Fixture/config/module.local.php',
],
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

return $application;

});

describe('/error-preview', function() {

it('empty as rely to original mvc process to handle', function() {

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('http://example.com/error-preview');
$request->setRequestUri('/error-preview');

\ob_start();
$this->application->run();
$content = \ob_get_clean();

expect(\ob_get_clean())->toBe('');
expect($this->application->getResponse()->getStatusCode())->toBe(500);

// $select = $this->tableGateway->getSql()->select();
// echo $select->count();

});

});

describe('/error-preview/notice', function() {

it('empty as rely to original mvc process to handle', function() {

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('http://example.com/error-preview/notice');
$request->setRequestUri('/error-preview/notice');

\ob_start();
$this->application->run();
$content = \ob_get_clean();

expect(\ob_get_clean())->toBe('');
$this->application->getResponse()->setStatusCode(http_response_code());
expect($this->application->getResponse()->getStatusCode())->toBe(500);


});

});

describe('/error-preview/error', function() {

it('empty as rely to original mvc process to handle', function() {

$request = $this->application->getRequest();
$request->setMethod('GET');
$request->setUri('http://example.com/error-preview/error');
$request->setRequestUri('/error-preview/error');

\ob_start();
$this->application->run();
$content = \ob_get_clean();

expect(\ob_get_clean())->toBe('');
expect($this->application->getResponse()->getStatusCode())->toBe(500);


});

});

});
2 changes: 1 addition & 1 deletion src/Handler/Formatter/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace ErrorHeroModule\Handler\Formatter;

use DateTime;
use Zend\Log\Formatter\Json as BaseJson;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Log\Formatter\Json as BaseJson;

class Json extends BaseJson implements FormatterInterface
{
Expand Down
18 changes: 18 additions & 0 deletions src/HeroFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
namespace ErrorHeroModule;

use Seld\JsonLint\JsonParser;
use Throwable;

function detectMessageContentType(string $message) : string
{
return (new JsonParser())->lint($message) === null
? 'application/problem+json'
: ((\strip_tags($message) === $message) ? 'text/plain' : 'text/html');
}

function isExcludedException(array $excludeExceptionsConfig, Throwable $t)
{
$exceptionOrErrorClass = \get_class($t);

foreach ($excludeExceptionsConfig as $excludeException) {
if ($exceptionOrErrorClass === $excludeException) {
return true;
}

if (is_array($excludeException) && $excludeException[0] === $exceptionOrErrorClass && $excludeException[1] === $t->getMessage()) {
return true;
}
}

return false;
}
11 changes: 9 additions & 2 deletions src/HeroTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ public function phpErrorHandler(int $errorType, string $errorMessage, string $er
return;
}

if (\in_array($errorType, $this->errorHeroModuleConfig['display-settings']['exclude-php-errors'])) {
return;
\http_response_code(500);
foreach ($this->errorHeroModuleConfig['display-settings']['exclude-php-errors'] as $excludePhpError) {
if ($errorType === $excludePhpError) {
return;
}

if (is_array($excludePhpError) && $excludePhpError[0] === $errorType && $excludePhpError[1] === $errorMessage) {
return;
}
}

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

0 comments on commit 358414a

Please sign in to comment.