Skip to content

Commit d4ccf1b

Browse files
committed
console exceptions handling
1 parent 801a0d9 commit d4ccf1b

File tree

1 file changed

+111
-27
lines changed

1 file changed

+111
-27
lines changed

core/src/ExceptionHandler.php

Lines changed: 111 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
use Illuminate\Contracts\Container\Container;
44
use AgelxNash\Modx\Evo\Database\Exceptions\ConnectException;
5+
use Symfony\Component\Console\Exception\CommandNotFoundException;
6+
use Symfony\Component\Console\Exception\InvalidArgumentException;
7+
use Symfony\Component\Console\Exception\InvalidOptionException;
8+
use Symfony\Component\Console\Exception\RuntimeException;
59
use Symfony\Component\ErrorHandler\Error\FatalError;
610
use Symfony\Component\ErrorHandler\Error\FatalError as FatalErrorException;
711
use EvolutionCMS\Providers\TracyServiceProvider;
@@ -22,16 +26,12 @@ public function __construct(Container $container)
2226
$this->container = $container;
2327
$this->container->register(TracyServiceProvider::class);
2428
if (!$this->container['config']->get('tracy.active')) {
25-
$this->registerHanlders();
29+
$this->registerHandlers();
2630
}
2731
}
2832

29-
protected function registerHanlders()
33+
protected function registerHandlers()
3034
{
31-
if (!defined('MODX_CLI') || MODX_CLI) {
32-
return;
33-
}
34-
3535
register_shutdown_function([$this, 'handleShutdown']);
3636
set_exception_handler([$this, 'handleException']);
3737
set_error_handler([$this, 'phpError']);
@@ -175,8 +175,6 @@ public function messageQuit(
175175

176176
$table = array();
177177

178-
$version = isset ($GLOBALS['modx_version']) ? $GLOBALS['modx_version'] : '';
179-
$release_date = isset ($GLOBALS['release_date']) ? $GLOBALS['release_date'] : '';
180178
if (isset($_SERVER['HTTP_HOST'])) {
181179
$request_uri = "http://" . $_SERVER['HTTP_HOST'] . ($_SERVER["SERVER_PORT"] == 80 ? "" : (":" . $_SERVER["SERVER_PORT"])) . $_SERVER['REQUEST_URI'];
182180
$request_uri = $this->container->getPhpCompat()->htmlspecialchars($request_uri, ENT_QUOTES,
@@ -317,7 +315,13 @@ public function messageQuit(
317315
if (!empty($php_errormsg) && isset($php_errormsg['message'])) {
318316
$str = '<b>' . $php_errormsg['message'] . '</b><br />' . PHP_EOL . $str;
319317
}
320-
$str .= $this->getBacktrace(empty($backtrace) ? debug_backtrace() : $backtrace);
318+
319+
if (empty($backtrace)) {
320+
$backtrace = debug_backtrace();
321+
}
322+
$backtrace = $this->prepareBacktrace($backtrace);
323+
$str .= $this->getBacktrace($backtrace);
324+
321325
// Log error
322326
if (!empty($this->container->currentSnippet)) {
323327
$source = 'Snippet - ' . $this->container->currentSnippet;
@@ -367,7 +371,47 @@ public function messageQuit(
367371
}
368372

369373
// Display error
370-
if ($this->shouldDisplay()) {
374+
if (is_cli()) {
375+
echo $msg, "\n\n";
376+
377+
if (!empty ($query)) {
378+
echo 'SQL: ', $query, "\n";
379+
}
380+
381+
if (!empty($nr) || !empty($file)) {
382+
if ($text != '') {
383+
echo 'Error: ', $text, "\n";
384+
}
385+
if ($output != '') {
386+
echo $output, "\n";
387+
}
388+
if ($nr !== '') {
389+
echo 'ErrorType[num]: ', $errortype [$nr] . "[$nr]", "\n";
390+
}
391+
if ($file) {
392+
echo 'File: ', $file, "\n";
393+
}
394+
if ($line) {
395+
echo 'Line: ', $line, "\n";
396+
}
397+
}
398+
399+
if ($source != '') {
400+
echo 'Source: ', $source, "\n";
401+
}
402+
403+
if (!empty($this->currentSnippet)) {
404+
echo 'Current Snippet: ', $this->currentSnippet, "\n";
405+
}
406+
407+
if (!empty($this->event->activePlugin)) {
408+
echo 'Current Plugin: ', $this->event->activePlugin . '(' . $this->event->name . ')', "\n";
409+
}
410+
411+
echo "\n", $this->getConsoleBacktrace($backtrace);
412+
} else if ($this->shouldDisplay()) {
413+
$version = isset($GLOBALS['modx_version']) ? $GLOBALS['modx_version'] : '';
414+
$release_date = isset($GLOBALS['release_date']) ? $GLOBALS['release_date'] : '';
371415

372416
echo '<!DOCTYPE html><html><head><title>Evolution CMS Content Manager ' . $version . ' &raquo; ' . $release_date . '</title>
373417
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
@@ -376,7 +420,6 @@ public function messageQuit(
376420
<style type="text/css">body { padding:10px; } td {font:inherit;}</style>
377421
</head><body>
378422
' . $str . '</body></html>';
379-
380423
} else {
381424
if (file_exists(EVO_CORE_PATH . 'custom/error_page.html')) {
382425
echo file_get_contents(EVO_CORE_PATH . 'custom/error_page.html');
@@ -394,18 +437,11 @@ protected function shouldDisplay()
394437
return isset($_SESSION['mgrValidated']) || $this->container['config']->get('app.debug');
395438
}
396439

397-
/**
398-
* @param $backtrace
399-
* @return string
400-
*/
401-
public function getBacktrace($backtrace)
440+
protected function prepareBacktrace($backtrace)
402441
{
403-
$MakeTable = $this->container->getService('makeTable');
404-
$MakeTable->setTableClass('grid');
405-
$MakeTable->setRowRegularClass('gridItem');
406-
$MakeTable->setRowAlternateClass('gridAltItem');
407-
$table = array();
442+
$result = [];
408443
$backtrace = array_reverse($backtrace);
444+
409445
foreach ($backtrace as $key => $val) {
410446
$key++;
411447
if (substr($val['function'], 0, 11) === 'messageQuit') {
@@ -475,16 +511,55 @@ public function getBacktrace($backtrace)
475511

476512
return $out;
477513
}, $args);
478-
$line = array(
479-
"<strong>" . $functionName . "</strong>(" . $args . ")",
480-
$path . " on line " . $val['line']
481-
);
482-
$table[] = array(implode("<br />", $line));
514+
515+
$result[] = [
516+
'func' => $functionName,
517+
'args' => $args,
518+
'path' => $path,
519+
'line' => $val['line'],
520+
];
521+
}
522+
523+
return $result;
524+
}
525+
526+
/**
527+
* @param $backtrace
528+
* @return string
529+
*/
530+
public function getBacktrace($backtrace)
531+
{
532+
$MakeTable = $this->container->getService('makeTable');
533+
$MakeTable->setTableClass('grid');
534+
$MakeTable->setRowRegularClass('gridItem');
535+
$MakeTable->setRowAlternateClass('gridAltItem');
536+
$table = array();
537+
538+
foreach ($backtrace as $line) {
539+
$table[] = array(implode("<br />", [
540+
"<strong>" . $line['func'] . "</strong>(" . $line['args'] . ")",
541+
$line['path'] . " on line " . $line['line'],
542+
]));
483543
}
484544

485545
return $MakeTable->create($table, array('Backtrace'));
486546
}
487547

548+
/**
549+
* @param $backtrace
550+
* @return string
551+
*/
552+
public function getConsoleBacktrace($backtrace)
553+
{
554+
$result = '';
555+
556+
foreach ($backtrace as $i => $line) {
557+
$result .= '#' . ($i + 1) . '. ' . $line['func'] . '(' . $line['args'] . '), ' . $line['path'] . ' on line ' . $line['line'] . "\n";
558+
}
559+
560+
return $result;
561+
}
562+
488563
/**
489564
* Determine if the exception should be reported.
490565
*
@@ -502,14 +577,23 @@ public function shouldReport(\Throwable $exception)
502577
*/
503578
public function handleException(\Throwable $exception)
504579
{
505-
506580
if (
507581
$exception instanceof ConnectException ||
508582
($exception instanceof \PDOException && $exception->getCode() === 1045)
509583
) {
510584
$this->container->getDatabase()->disconnect();
511585
}
512586

587+
if (is_cli() && (
588+
$exception instanceof RuntimeException ||
589+
$exception instanceof InvalidArgumentException ||
590+
$exception instanceof InvalidOptionException ||
591+
$exception instanceof CommandNotFoundException
592+
)) {
593+
echo $exception->getMessage();
594+
exit;
595+
}
596+
513597
$this->messageQuit(
514598
$exception->getMessage(),
515599
'',

0 commit comments

Comments
 (0)