Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and beautify error_exception.php for CLI #3367

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 68 additions & 13 deletions app/Views/errors/cli/error_exception.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
An uncaught Exception was encountered
<?php

Type: <?= get_class($exception), "\n"; ?>
Message: <?= $message, "\n"; ?>
Filename: <?= $exception->getFile(), "\n"; ?>
Line Number: <?= $exception->getLine(); ?>
use CodeIgniter\CLI\CLI;

<?php if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE === true): ?>
// The main Exception
CLI::newLine();
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
CLI::newLine();
CLI::write($message);
CLI::newLine();
CLI::write('at ' . CLI::color(clean_path($exception->getFile()) . ':' . $exception->getLine(), 'green'));
CLI::newLine();

Backtrace:
<?php foreach ($exception->getTrace() as $error): ?>
<?php if (isset($error['file'])): ?>
<?= trim('-' . $error['line'] . ' - ' . $error['file'] . '::' . $error['function']) . "\n" ?>
<?php endif ?>
<?php endforeach ?>
// The backtrace
if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE)
{
$backtraces = $exception->getTrace();

<?php endif ?>
if ($backtraces)
{
CLI::write('Backtrace:', 'green');
}

foreach ($backtraces as $i => $error)
{
$padFile = ' '; // 4 spaces
$padClass = ' '; // 7 spaces
$c = str_pad($i + 1, 3, ' ', STR_PAD_LEFT);

if (isset($error['file']))
{
$filepath = clean_path($error['file']) . ':' . $error['line'];

CLI::write($c . $padFile . CLI::color($filepath, 'yellow'));
}
else
{
CLI::write($c . $padFile . CLI::color('[internal function]', 'yellow'));
}

$function = '';

if (isset($error['class']))
{
$type = ($error['type'] === '->') ? '()' . $error['type'] : $error['type'];
$function .= $padClass . $error['class'] . $type . $error['function'];
}
elseif (! isset($error['class']) && isset($error['function']))
{
$function .= $padClass . $error['function'];
}

$args = implode(', ', array_map(function ($value) {
switch (true)
{
case is_object($value):
return 'Object(' . get_class($value) . ')';
case is_array($value):
return '[]';
case is_null($value):
return 'null'; // return the lowercased version
default:
return var_export($value, true);
}
}, array_values($error['args'])));

$function .= '(' . $args . ')';

CLI::write($function);
CLI::newLine();
}
}