error_exception.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. use CodeIgniter\CLI\CLI;
  3. // The main Exception
  4. CLI::write('[' . $exception::class . ']', 'light_gray', 'red');
  5. CLI::write($message);
  6. CLI::write('at ' . CLI::color(clean_path($exception->getFile()) . ':' . $exception->getLine(), 'green'));
  7. CLI::newLine();
  8. $last = $exception;
  9. while ($prevException = $last->getPrevious()) {
  10. $last = $prevException;
  11. CLI::write(' Caused by:');
  12. CLI::write(' [' . $prevException::class . ']', 'red');
  13. CLI::write(' ' . $prevException->getMessage());
  14. CLI::write(' at ' . CLI::color(clean_path($prevException->getFile()) . ':' . $prevException->getLine(), 'green'));
  15. CLI::newLine();
  16. }
  17. // The backtrace
  18. if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) {
  19. $backtraces = $last->getTrace();
  20. if ($backtraces) {
  21. CLI::write('Backtrace:', 'green');
  22. }
  23. foreach ($backtraces as $i => $error) {
  24. $padFile = ' '; // 4 spaces
  25. $padClass = ' '; // 7 spaces
  26. $c = str_pad($i + 1, 3, ' ', STR_PAD_LEFT);
  27. if (isset($error['file'])) {
  28. $filepath = clean_path($error['file']) . ':' . $error['line'];
  29. CLI::write($c . $padFile . CLI::color($filepath, 'yellow'));
  30. } else {
  31. CLI::write($c . $padFile . CLI::color('[internal function]', 'yellow'));
  32. }
  33. $function = '';
  34. if (isset($error['class'])) {
  35. $type = ($error['type'] === '->') ? '()' . $error['type'] : $error['type'];
  36. $function .= $padClass . $error['class'] . $type . $error['function'];
  37. } elseif (! isset($error['class']) && isset($error['function'])) {
  38. $function .= $padClass . $error['function'];
  39. }
  40. $args = implode(', ', array_map(static fn ($value): string => match (true) {
  41. is_object($value) => 'Object(' . $value::class . ')',
  42. is_array($value) => $value !== [] ? '[...]' : '[]',
  43. $value === null => 'null', // return the lowercased version
  44. default => var_export($value, true),
  45. }, array_values($error['args'] ?? [])));
  46. $function .= '(' . $args . ')';
  47. CLI::write($function);
  48. CLI::newLine();
  49. }
  50. }