error.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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'.DS.'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. $response_body = "<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. $response_body .= "
  35. <h3>Stack Trace:</h3>
  36. <pre>".$exception->getTraceAsString()."</pre></html>";
  37. }
  38. $response = Response::make($response_body, 500);
  39. }
  40. // If we're not using detailed error messages, we'll use the event
  41. // system to get the response that should be sent to the browser.
  42. // Using events gives the developer more freedom.
  43. else
  44. {
  45. $response = Event::first('500', $exception);
  46. $response = Response::prepare($response);
  47. }
  48. $response->render();
  49. $response->send();
  50. $response->foundation->finish();
  51. exit(1);
  52. }
  53. /**
  54. * Handle a native PHP error as an ErrorException.
  55. *
  56. * @param int $code
  57. * @param string $error
  58. * @param string $file
  59. * @param int $line
  60. * @return void
  61. */
  62. public static function native($code, $error, $file, $line)
  63. {
  64. if (error_reporting() === 0) return;
  65. // For a PHP error, we'll create an ErrorException and then feed that
  66. // exception to the exception method, which will create a simple view
  67. // of the exception details for the developer.
  68. $exception = new \ErrorException($error, $code, 0, $file, $line);
  69. if (in_array($code, Config::get('error.ignore')))
  70. {
  71. return static::log($exception);
  72. }
  73. static::exception($exception);
  74. }
  75. /**
  76. * Handle the PHP shutdown event.
  77. *
  78. * @return void
  79. */
  80. public static function shutdown()
  81. {
  82. // If a fatal error occurred that we have not handled yet, we will
  83. // create an ErrorException and feed it to the exception handler,
  84. // as it will not yet have been handled.
  85. $error = error_get_last();
  86. if ( ! is_null($error))
  87. {
  88. extract($error, EXTR_SKIP);
  89. static::exception(new \ErrorException($message, $type, 0, $file, $line), false);
  90. }
  91. }
  92. /**
  93. * Log an exception.
  94. *
  95. * @param Exception $exception
  96. * @return void
  97. */
  98. public static function log($exception)
  99. {
  100. if (Config::get('error.log'))
  101. {
  102. call_user_func(Config::get('error.logger'), $exception);
  103. }
  104. }
  105. }