error.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php namespace Laravel;
  2. class Error {
  3. /**
  4. * Handle an exception and display the exception report.
  5. *
  6. * @param Exception $exception
  7. * @return void
  8. */
  9. public static function exception($exception)
  10. {
  11. static::log($exception);
  12. // If detailed errors are enabled, we'll just format the exception into
  13. // a simple error message and display it on the screen. We don't use a
  14. // View in case the problem is in the View class itself so we can not
  15. // run into a white screen of death situation.
  16. if (Config::get('error.detail'))
  17. {
  18. echo "<html><h2>Unhandled Exception</h2>
  19. <h3>Message:</h3>
  20. <pre>".$exception->getMessage()."</pre>
  21. <h3>Location:</h3>
  22. <pre>".$exception->getFile()." on line ".$exception->getLine()."</pre>
  23. <h3>Stack Trace:</h3>
  24. <pre>".$exception->getTraceAsString()."</pre></html>";
  25. }
  26. else
  27. {
  28. Response::error('500')->send();
  29. }
  30. exit(1);
  31. }
  32. /**
  33. * Handle a native PHP error as an ErrorException.
  34. *
  35. * @param int $code
  36. * @param string $error
  37. * @param string $file
  38. * @param int $line
  39. * @return void
  40. */
  41. public static function native($code, $error, $file, $line)
  42. {
  43. if (error_reporting() === 0) return;
  44. // For a PHP error, we'll create an ErrorExcepetion and then feed that
  45. // exception to the exception method, which will create a simple view
  46. // of the exception details. The ErrorException class is built-in to
  47. // PHP for converting native errors to Exceptions.
  48. $exception = new \ErrorException($error, $code, 0, $file, $line);
  49. if (in_array($code, Config::get('error.ignore')))
  50. {
  51. return static::log($exception);
  52. }
  53. static::exception($exception);
  54. }
  55. /**
  56. * Handle the PHP shutdown event.
  57. *
  58. * @return void
  59. */
  60. public static function shutdown()
  61. {
  62. // If a fatal error occured that we have not handled yet, we will
  63. // create an ErrorException and feed it to the exception handler,
  64. // as it will not have been handled by the error handler.
  65. if ( ! is_null($error = error_get_last()))
  66. {
  67. extract($error, EXTR_SKIP);
  68. static::exception(new \ErrorException($message, $type, 0, $file, $line));
  69. }
  70. }
  71. /**
  72. * Log an exception.
  73. *
  74. * @param Exception $exception
  75. * @return void
  76. */
  77. public static function log($exception)
  78. {
  79. if (Config::get('error.log'))
  80. {
  81. call_user_func(Config::get('error.logger'), $exception);
  82. }
  83. }
  84. }