error.php 2.4 KB

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