From 0b0eed0b5ab64510768cb24e15f70262a7c0bdcd Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 20 Jul 2020 23:13:16 +0800 Subject: [PATCH] Refactor error_exception.php for CLI --- app/Views/errors/cli/error_exception.php | 81 ++++++++++++++++++++---- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/app/Views/errors/cli/error_exception.php b/app/Views/errors/cli/error_exception.php index f8f2d9517799..92e8ab06aac6 100644 --- a/app/Views/errors/cli/error_exception.php +++ b/app/Views/errors/cli/error_exception.php @@ -1,17 +1,72 @@ -An uncaught Exception was encountered + -Message: -Filename: getFile(), "\n"; ?> -Line Number: getLine(); ?> +use CodeIgniter\CLI\CLI; - +// 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: - getTrace() as $error): ?> - - - - +// The backtrace +if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) +{ + $backtraces = $exception->getTrace(); - + 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(); + } +}