1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-16 11:30:55 +01:00

Generalizzazione errori AJAX

This commit is contained in:
Thomas Zilio 2019-01-11 12:02:11 +01:00
parent 3d12041beb
commit 296d41cfe6
4 changed files with 60 additions and 57 deletions

109
core.php
View File

@ -30,23 +30,6 @@ foreach ($namespaces as $path => $namespace) {
$loader->addPsr4($namespace.'\\', __DIR__.'/'.$path.'/src');
}
// Inclusione dei file modutil.php
// TODO: sostituire * con lista module dir {aggiornamenti,anagrafiche,articoli}
// TODO: sostituire tutte le funzioni dei moduli con classi Eloquent relative
$files = glob(__DIR__.'/{modules,plugins}/*/modutil.php', GLOB_BRACE);
$custom_files = glob(__DIR__.'/{modules,plugins}/*/custom/modutil.php', GLOB_BRACE);
foreach ($custom_files as $key => $value) {
$index = array_search(str_replace('custom/', '', $value), $files);
if ($index !== false) {
unset($files[$index]);
}
}
$list = array_merge($files, $custom_files);
foreach ($list as $file) {
include_once $file;
}
// Individuazione dei percorsi di base
App::definePaths(__DIR__);
@ -63,9 +46,6 @@ if (!empty($config['redirectHTTPS']) && !isHTTPS(true)) {
exit();
}
// Forza l'abilitazione del debug
// $debug = App::debug(true);
/* GESTIONE DEGLI ERRORI */
// Logger per la segnalazione degli errori
$logger = new Monolog\Logger('Logs');
@ -81,55 +61,44 @@ use Monolog\Handler\StreamHandler;
$handlers = [];
if (!API::isAPIRequest()) {
// File di log di base (logs/error.log)
// File di log di base (logs/error.log, logs/setup.log)
$handlers[] = new StreamHandler($docroot.'/logs/error.log', Monolog\Logger::ERROR);
$handlers[] = new StreamHandler($docroot.'/logs/setup.log', Monolog\Logger::EMERGENCY);
// Messaggi grafici per l'utente
$handlers[] = new Extensions\MessageHandler(Monolog\Logger::ERROR);
// Impostazioni di debug
// File di log ordinati in base alla data
if (App::debug()) {
// Ignora gli avvertimenti e le informazioni relative alla deprecazione di componenti
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_USER_DEPRECATED & ~E_STRICT);
// File di log ordinato in base alla data
$handlers[] = new RotatingFileHandler($docroot.'/logs/error.log', 0, Monolog\Logger::ERROR);
$handlers[] = new RotatingFileHandler($docroot.'/logs/setup.log', 0, Monolog\Logger::EMERGENCY);
$prettyPageHandler = new Whoops\Handler\PrettyPageHandler();
// Imposta Whoops come gestore delle eccezioni di default
$whoops = new Whoops\Run();
$whoops->pushHandler($prettyPageHandler);
// Abilita la gestione degli errori nel caso la richiesta sia di tipo AJAX
if (Whoops\Util\Misc::isAjaxRequest()) {
$whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
}
$whoops->register();
}
// Inizializzazione Whoops
$whoops = new Whoops\Run();
if (App::debug() || isAjaxRequest()) {
$whoops->pushHandler(new Whoops\Handler\PrettyPageHandler());
}
// Abilita la gestione degli errori nel caso la richiesta sia di tipo AJAX
if (isAjaxRequest()) {
$whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
}
$whoops->register();
} else {
$handlers[] = new StreamHandler($docroot.'/logs/api.log', Monolog\Logger::ERROR);
}
// Disabilita la segnalazione degli errori (se il debug è disabilitato)
if (!App::debug()) {
error_reporting(0);
ini_set('display_errors', 0);
}
// Imposta il formato di salvataggio dei log
$pattern = '[%datetime%] %channel%.%level_name%: %message%';
if (App::debug()) {
$pattern .= ' %context%';
}
$pattern .= PHP_EOL.'%extra% '.PHP_EOL;
// Disabilita i messaggi nativi di PHP
ini_set('display_errors', 0);
// Ignora gli avvertimenti e le informazioni relative alla deprecazione di componenti
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_USER_DEPRECATED & ~E_STRICT);
$pattern = '[%datetime%] %channel%.%level_name%: %message% %context%'.PHP_EOL.'%extra% '.PHP_EOL;
$monologFormatter = new Monolog\Formatter\LineFormatter($pattern);
if (App::debug()) {
$monologFormatter->includeStacktraces(true);
}
$monologFormatter->includeStacktraces(App::debug());
// Filtra gli errori per livello preciso del gestore dedicato
foreach ($handlers as $handler) {
@ -140,6 +109,19 @@ foreach ($handlers as $handler) {
// Imposta Monolog come gestore degli errori
Monolog\ErrorHandler::register($logger, [], Monolog\Logger::ERROR, Monolog\Logger::ERROR);
// Aggiunta di Monolog a Whoops
if (App::debug()) {
$whoops->pushHandler(function (\Whoops\Exception\ErrorException $exception, $inspector, $run) use ($logger) {
$logger->addError($exception->getMessage(), [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => null,
]);
});
}
// Database
$dbo = $database = database();
@ -281,3 +263,20 @@ if (!API::isAPIRequest()) {
$post = Filter::getPOST();
$get = Filter::getGET();
}
// Inclusione dei file modutil.php
// TODO: sostituire * con lista module dir {aggiornamenti,anagrafiche,articoli}
// TODO: sostituire tutte le funzioni dei moduli con classi Eloquent relative
$files = glob(__DIR__.'/{modules,plugins}/*/modutil.php', GLOB_BRACE);
$custom_files = glob(__DIR__.'/{modules,plugins}/*/custom/modutil.php', GLOB_BRACE);
foreach ($custom_files as $key => $value) {
$index = array_search(str_replace('custom/', '', $value), $files);
if ($index !== false) {
unset($files[$index]);
}
}
$list = array_merge($files, $custom_files);
foreach ($list as $file) {
include_once $file;
}

View File

@ -5,7 +5,7 @@ include_once __DIR__.'/../core.php';
$paths = App::getPaths();
$user = Auth::user();
$pageTitle = $pageTitle ?: $structure->title;
$pageTitle = !empty($pageTitle) ? $pageTitle : $structure->title;
$messages = flash()->getMessages();

View File

@ -275,7 +275,7 @@ function slashes($string)
*/
function isAjaxRequest()
{
return \Whoops\Util\Misc::isAjaxRequest() && filter('ajax') !== null;
return \Whoops\Util\Misc::isAjaxRequest();
}
/**

View File

@ -13,6 +13,10 @@ class MessageHandler extends AbstractProcessingHandler
{
protected function write(array $record)
{
if (isAjaxRequest()) {
return;
}
$message = tr("Si è verificato un'errore").' <i>[uid: '.$record['extra']['uid'].']</i>.';
if (auth()->check()) {