error.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return true;
  53. }
  54. static::exception($exception);
  55. }
  56. /**
  57. * Handle the PHP shutdown event.
  58. *
  59. * @return void
  60. */
  61. public static function shutdown()
  62. {
  63. // If a fatal error occured that we have not handled yet, we will
  64. // create an ErrorException and feed it to the exception handler,
  65. // as it will not have been handled by the error handler.
  66. if ( ! is_null($error = error_get_last()))
  67. {
  68. extract($error, EXTR_SKIP);
  69. static::exception(new \ErrorException($message, $type, 0, $file, $line));
  70. }
  71. }
  72. /**
  73. * Log an exception.
  74. *
  75. * @param Exception $exception
  76. * @return void
  77. */
  78. public static function log($exception)
  79. {
  80. if (Config::get('error.log'))
  81. {
  82. call_user_func(Config::get('error.logger'), $exception);
  83. }
  84. }
  85. }