error.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. else
  26. {
  27. Response::error('500')->send();
  28. }
  29. exit(1);
  30. }
  31. /**
  32. * Handle a native PHP error as an ErrorException.
  33. *
  34. * @param int $code
  35. * @param string $error
  36. * @param string $file
  37. * @param int $line
  38. * @return void
  39. */
  40. public static function native($code, $error, $file, $line)
  41. {
  42. if (error_reporting() === 0) return;
  43. // For a PHP error, we'll create an ErrorExcepetion and then feed that
  44. // exception to the exception method, which will create a simple view
  45. // of the exception details. The ErrorException class is built-in to
  46. // PHP for converting native errors.
  47. $exception = new \ErrorException($error, $code, 0, $file, $line);
  48. if (in_array($code, Config::get('error.ignore')))
  49. {
  50. return static::log($exception);
  51. return true;
  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. }