error.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php namespace Laravel;
  2. class Error {
  3. /**
  4. * Handle an exception and display the exception report.
  5. *
  6. * @param Exception $exception
  7. * @param bool $trace
  8. * @return void
  9. */
  10. public static function exception($exception, $trace = true)
  11. {
  12. static::log($exception);
  13. ob_get_level() and ob_end_clean();
  14. $message = $exception->getMessage();
  15. // For Laravel view errors we want to show a prettier error:
  16. $file = $exception->getFile();
  17. if (str_contains($exception->getFile(), 'eval()') and str_contains($exception->getFile(), 'laravel/view.php'))
  18. {
  19. $message = 'Error rendering view: ['.View::$last['name'].']'.PHP_EOL.PHP_EOL.$message;
  20. $file = View::$last['path'];
  21. }
  22. // If detailed errors are enabled, we'll just format the exception into
  23. // a simple error message and display it on the screen. We don't use a
  24. // View in case the problem is in the View class.
  25. if (Config::get('error.detail'))
  26. {
  27. echo "<html><h2>Unhandled Exception</h2>
  28. <h3>Message:</h3>
  29. <pre>".$message."</pre>
  30. <h3>Location:</h3>
  31. <pre>".$file." on line ".$exception->getLine()."</pre>";
  32. if ($trace)
  33. {
  34. echo "
  35. <h3>Stack Trace:</h3>
  36. <pre>".$exception->getTraceAsString()."</pre></html>";
  37. }
  38. }
  39. // If we're not using detailed error messages, we'll use the event
  40. // system to get the response that should be sent to the browser.
  41. // Using events gives the developer more freedom.
  42. else
  43. {
  44. $response = Event::first('500');
  45. echo Response::prepare($response)->render();
  46. }
  47. exit(1);
  48. }
  49. /**
  50. * Handle a native PHP error as an ErrorException.
  51. *
  52. * @param int $code
  53. * @param string $error
  54. * @param string $file
  55. * @param int $line
  56. * @return void
  57. */
  58. public static function native($code, $error, $file, $line)
  59. {
  60. if (error_reporting() === 0) return;
  61. // For a PHP error, we'll create an ErrorException and then feed that
  62. // exception to the exception method, which will create a simple view
  63. // of the exception details for the developer.
  64. $exception = new \ErrorException($error, $code, 0, $file, $line);
  65. if (in_array($code, Config::get('error.ignore')))
  66. {
  67. return static::log($exception);
  68. }
  69. static::exception($exception);
  70. }
  71. /**
  72. * Handle the PHP shutdown event.
  73. *
  74. * @return void
  75. */
  76. public static function shutdown()
  77. {
  78. // If a fatal error occurred that we have not handled yet, we will
  79. // create an ErrorException and feed it to the exception handler,
  80. // as it will not yet have been handled.
  81. $error = error_get_last();
  82. if ( ! is_null($error))
  83. {
  84. extract($error, EXTR_SKIP);
  85. static::exception(new \ErrorException($message, $type, 0, $file, $line), false);
  86. }
  87. }
  88. /**
  89. * Log an exception.
  90. *
  91. * @param Exception $exception
  92. * @return void
  93. */
  94. public static function log($exception)
  95. {
  96. if (Config::get('error.log'))
  97. {
  98. call_user_func(Config::get('error.logger'), $exception);
  99. }
  100. }
  101. }