error.php 2.5 KB

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