Skip to content

Commit 576fcd0

Browse files
Pierstovalcedric-anne
authored andcommitted
Symfony compliant error handling
1 parent b52e5de commit 576fcd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1358
-1383
lines changed

.phpstan-baseline.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,12 +1735,6 @@
17351735
'count' => 1,
17361736
'path' => __DIR__ . '/src/Fieldblacklist.php',
17371737
];
1738-
$ignoreErrors[] = [
1739-
'message' => '#^Property GLPI\\:\\:\\$error_handler is unused\\.$#',
1740-
'identifier' => 'property.unused',
1741-
'count' => 1,
1742-
'path' => __DIR__ . '/src/GLPI.php',
1743-
];
17441738
$ignoreErrors[] = [
17451739
'message' => '#^Method GLPIKey\\:\\:keyExists\\(\\) should return string but returns bool\\.$#',
17461740
'identifier' => 'return.type',
@@ -2065,24 +2059,6 @@
20652059
'count' => 1,
20662060
'path' => __DIR__ . '/src/Glpi/Api/HL/Search.php',
20672061
];
2068-
$ignoreErrors[] = [
2069-
'message' => '#^Instanceof between Psr\\\\Log\\\\LoggerInterface and Psr\\\\Log\\\\LoggerInterface will always evaluate to true\\.$#',
2070-
'identifier' => 'instanceof.alwaysTrue',
2071-
'count' => 1,
2072-
'path' => __DIR__ . '/src/Glpi/Application/ErrorHandler.php',
2073-
];
2074-
$ignoreErrors[] = [
2075-
'message' => '#^Property Glpi\\\\Application\\\\ErrorHandler\\:\\:\\$exit_code is never read, only written\\.$#',
2076-
'identifier' => 'property.onlyWritten',
2077-
'count' => 1,
2078-
'path' => __DIR__ . '/src/Glpi/Application/ErrorHandler.php',
2079-
];
2080-
$ignoreErrors[] = [
2081-
'message' => '#^Strict comparison using \\!\\=\\= between null and \'comment\'\\|\'error\' will always evaluate to true\\.$#',
2082-
'identifier' => 'notIdentical.alwaysTrue',
2083-
'count' => 1,
2084-
'path' => __DIR__ . '/src/Glpi/Application/ErrorHandler.php',
2085-
];
20862062
$ignoreErrors[] = [
20872063
'message' => '#^Strict comparison using \\=\\=\\= between null and 2 will always evaluate to false\\.$#',
20882064
'identifier' => 'identical.alwaysFalse',

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ The present file will list all changes made to the project; according to the
166166
- `Glpi\Dashboard\Filters\AbstractFilter::field()` method has been made protected.
167167
- Usage of `CommonITILValidation::dropdownValidator()` with the `name` and `users_id_validate` options are no longer supported. Use `prefix` and `itemtype_target`/`items_id_target` respectively instead.
168168
- The `helper` property of form fields will not support anymore the presence of HTML code.
169-
- `Glpi\Application\ErrorHandler` constructor visibility has been changed to private.
170169
- `GLPI::initErrorHandler()` does not return any value anymore.
171170
- The `inc/autoload.function.php`, `inc/based_config.php`, `inc/config.php`, `inc/db.function.php` and `inc/define.php` files have been removed and the `inc/includes.php` file has been almost emptied.
172171
The corresponding global functions, constants and variables are now loaded and initialized automatically and the corresponding GLPI boostraping logic is now executed automatically.
@@ -397,11 +396,11 @@ The present file will list all changes made to the project; according to the
397396
- `FieldUnicity::showDebug()`
398397
- `GLPI::getErrorHandler()`
399398
- `GLPI::getLogLevel()`
399+
- `GLPI::initErrorHandler()`
400+
- `GLPI::initLogger()`
400401
- `Glpi\Api\API::showDebug()`
401402
- `Glpi\Api\API::returnSanitizedContent()`
402-
- `Glpi\Application\ErrorHandler::handleSqlError()`
403-
- `Glpi\Application\ErrorHandler::handleSqlWarnings()`
404-
- `Glpi\Application\ErrorHandler::handleTwigError()`
403+
- `Glpi\Application\ErrorHandler` class
405404
- `Glpi\Console\Command\ForceNoPluginsOptionCommandInterface` class
406405
- `Glpi\Dashboard\Filter::dates()`
407406
- `Glpi\Dashboard\Filter::dates_mod()`

ajax/dashboard.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
3736
use Glpi\Dashboard\Grid;
37+
use Glpi\Error\ErrorHandler;
3838
use Glpi\Exception\Http\AccessDeniedHttpException;
3939

4040
if (!isset($_REQUEST["action"])) {
@@ -208,8 +208,7 @@
208208
$result[$card['card_id']] = $grid->getCardHtml($card['card_id'], array_merge($request_data, $card));
209209
} catch (\Throwable $e) {
210210
// Send exception to logger without actually exiting.
211-
// Use quiet mode to not break JSON result.
212-
ErrorHandler::getInstance()->handleException($e, true);
211+
ErrorHandler::logCaughtException($e);
213212
}
214213
}
215214
\Glpi\Debug\Profiler::getInstance()->stop('Get cards HTML');

ajax/debug.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
3736
use Glpi\Exception\Http\AccessDeniedHttpException;
3837
use Glpi\Exception\Http\BadRequestHttpException;
3938
use Glpi\Exception\Http\NotFoundHttpException;
@@ -99,8 +98,6 @@
9998
echo '[]';
10099
return;
101100
}
102-
// In some cases, a class that isn't a proper itemtype may show in the selection box and this would trigger a SQL error that cannot be caught.
103-
ErrorHandler::getInstance()->disableOutput();
104101
try {
105102
/** @var CommonGLPI $item */
106103
$item = new $_GET['itemtype']();

ajax/fileupload.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,4 @@
3737
* @since 9.2
3838
*/
3939

40-
use Glpi\Application\ErrorHandler;
41-
42-
// Ensure warnings will not break ajax output.
43-
ErrorHandler::getInstance()->disableOutput();
44-
4540
GLPIUploadHandler::uploadFiles($_POST);

dependency_injection/services.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
3636

3737
use Glpi\DependencyInjection\PublicService;
38-
use Glpi\Log\LegacyGlobalLogger;
38+
use Glpi\Error\ErrorHandler;
3939

4040
return static function (ContainerConfigurator $container): void {
4141
$projectDir = dirname(__DIR__);
@@ -47,6 +47,9 @@
4747
$parameters->set('env(APP_SECRET_FILE)', $projectDir . '/config/glpicrypt.key');
4848
$parameters->set('kernel.secret', env('default:glpi.default_secret:file:APP_SECRET_FILE'));
4949

50+
// Prevent low level errors (e.g. warning) to be converted to exception in dev environment
51+
$parameters->set('debug.error_handler.throw_at', ErrorHandler::FATAL_ERRORS);
52+
5053
$services = $services
5154
->defaults()
5255
->autowire()
@@ -64,9 +67,7 @@
6467
$projectDir . '/src/Glpi/Form/ConditionalVisiblity/*Manager.php'
6568
);
6669

67-
/**
68-
* Override Symfony's logger.
69-
* @see \Symfony\Component\HttpKernel\DependencyInjection\LoggerPass
70-
*/
71-
$services->set('logger', LegacyGlobalLogger::class);
70+
// Prevent Symfony to register its own default logger.
71+
// @see \Symfony\Component\HttpKernel\DependencyInjection\LoggerPass
72+
$services->set('logger')->synthetic();
7273
};

front/css.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
3736
use Glpi\UI\ThemeManager;
3837

3938
// Main CSS compilation requires about 70MB of memory.
@@ -43,9 +42,6 @@
4342
ini_set('memory_limit', sprintf('%dM', $max_memory));
4443
}
4544

46-
// Ensure warnings will not break CSS output.
47-
ErrorHandler::getInstance()->disableOutput();
48-
4945
// If a custom theme is requested, we need to get the real path of the theme
5046
if (isset($_GET['file']) && isset($_GET['is_custom_theme']) && $_GET['is_custom_theme']) {
5147
$theme = ThemeManager::getInstance()->getTheme($_GET['file']);

front/locale.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
36+
use Glpi\Error\ErrorHandler;
3737

3838
/**
3939
* @var array $CFG_GLPI
@@ -78,7 +78,7 @@
7878
$messages = $TRANSLATE->getAllMessages($_GET['domain']);
7979
} catch (\Throwable $e) {
8080
// Error may happen when overrided translation files does not use same plural rules as GLPI.
81-
ErrorHandler::getInstance()->handleException($e, true);
81+
ErrorHandler::logCaughtException($e);
8282
}
8383
if (!($messages instanceof \Laminas\I18n\Translator\TextDomain)) {
8484
// No TextDomain found means that there is no translations for given domain.

front/notificationmailingsetting.form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
36+
use Glpi\Error\ErrorHandler;
3737
use Glpi\Event;
3838
use Glpi\Mail\SMTP\OauthConfig;
3939

@@ -58,7 +58,7 @@
5858
$_SESSION['smtp_oauth2_state'] = $provider->getState();
5959
Html::redirect($auth_url);
6060
} catch (\Throwable $e) {
61-
ErrorHandler::getInstance()->handleException($e, true);
61+
ErrorHandler::logCaughtException($e);
6262
Session::addMessageAfterRedirect(
6363
htmlescape(sprintf(_x('oauth', 'Authorization failed with error: %s'), $e->getMessage())),
6464
false,

front/palette_preview.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@
3333
* ---------------------------------------------------------------------
3434
*/
3535

36-
use Glpi\Application\ErrorHandler;
3736
use Glpi\UI\ThemeManager;
3837

39-
// Ensure warnings will not break image output.
40-
ErrorHandler::getInstance()->disableOutput();
41-
4238
$theme = ThemeManager::getInstance()->getTheme($_GET['key']);
4339
$preview = $theme !== null ? $theme->getPreviewPath(false) : null;
4440

0 commit comments

Comments
 (0)