errors.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php namespace Laravel;
  2. /**
  3. * Define a closure that will return a formatted error message
  4. * when given an exception. This function will be used by the
  5. * error handler to create a more readable message.
  6. */
  7. $message = function($e)
  8. {
  9. $search = array(APP_PATH, SYS_PATH);
  10. $replace = array('APP_PATH/', 'SYS_PATH/');
  11. $file = str_replace($search, $replace, $e->getFile());
  12. return rtrim($e->getMessage(), '.').' in '.$file.' on line '.$e->getLine().'.';
  13. };
  14. /**
  15. * Define a clousre that will return a more readable version
  16. * of the severity of an exception. This function will be used
  17. * by the error handler when parsing exceptions.
  18. */
  19. $severity = function($e)
  20. {
  21. $levels = array(
  22. 0 => 'Error',
  23. E_ERROR => 'Error',
  24. E_WARNING => 'Warning',
  25. E_PARSE => 'Parsing Error',
  26. E_NOTICE => 'Notice',
  27. E_CORE_ERROR => 'Core Error',
  28. E_CORE_WARNING => 'Core Warning',
  29. E_COMPILE_ERROR => 'Compile Error',
  30. E_COMPILE_WARNING => 'Compile Warning',
  31. E_USER_ERROR => 'User Error',
  32. E_USER_WARNING => 'User Warning',
  33. E_USER_NOTICE => 'User Notice',
  34. E_STRICT => 'Runtime Notice',
  35. );
  36. if (array_key_exists($e->getCode(), $levels))
  37. {
  38. $level = $levels[$e->getCode()];
  39. }
  40. else
  41. {
  42. $level = $e->getCode();
  43. }
  44. return $level;
  45. };
  46. /**
  47. * Create the exception handler function. All of the error handlers
  48. * registered by the framework call this closure to avoid duplicate
  49. * code. Each of the formatting closures defined above will be
  50. * passed into the handler for convenient use.
  51. */
  52. $handler = function($e) use ($message, $severity)
  53. {
  54. $config = Config::get('error');
  55. if ($config['log'])
  56. {
  57. call_user_func($config['logger'], $e, $severity($e), $message($e), $config);
  58. }
  59. call_user_func($config['handler'], $e, $severity($e), $message($e), $config);
  60. exit(1);
  61. };
  62. /**
  63. * Register the PHP exception handler. The framework throws exceptions
  64. * on every error that cannot be handled. All of those exceptions will
  65. * fall into this closure for processing.
  66. */
  67. set_exception_handler(function($e) use ($handler)
  68. {
  69. $handler($e);
  70. });
  71. /**
  72. * Register the PHP error handler. All PHP errors will fall into this
  73. * handler, which will convert the error into an ErrorException object
  74. * and pass the exception into the common exception handler.
  75. */
  76. set_error_handler(function($number, $error, $file, $line) use ($handler)
  77. {
  78. $handler(new \ErrorException($error, $number, 0, $file, $line));
  79. });
  80. /**
  81. * Register the PHP shutdown handler. This function will be called at
  82. * the end of the PHP script or on a fatal PHP error. If an error has
  83. * occurred, we will convert it to an ErrorException and pass it to
  84. * the common exception handler.
  85. */
  86. register_shutdown_function(function() use ($handler)
  87. {
  88. if ( ! is_null($error = error_get_last()))
  89. {
  90. die('here');
  91. extract($error, EXTR_SKIP);
  92. $handler(new \ErrorException($message, $type, 0, $file, $line));
  93. }
  94. });
  95. /**
  96. * Turn off all PHP error reporting and display. Since the framework
  97. * will be displaying the exception messages, we don't want PHP to
  98. * display any ugly error information.
  99. */
  100. error_reporting(-1);
  101. ini_set('display_errors', 'Off');