errorhandler.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. error_reporting(E_ALL);
  3. //ini_set('display_errors', 'Off');
  4. function error_handle_type($type=null){
  5. static $etype;
  6. if(!$etype){
  7. $etype = 'html';
  8. }
  9. if(!empty($type)){
  10. $etype = $type;
  11. }
  12. return $etype;
  13. }
  14. function handle_error($error,$backtrace,$included){
  15. $context = array(
  16. 'error'=>$error,
  17. 'backtrace'=>$backtrace,
  18. 'included'=>$included
  19. );
  20. Router::clear();
  21. switch(error_handle_type()){
  22. case 'json':
  23. Router::json($context);
  24. break;
  25. case 'html':default:
  26. Router::write(
  27. Bugs::template('error')
  28. ->run(new Arguments($context))
  29. );
  30. }
  31. die();
  32. }
  33. set_error_handler(function($errno, $errstr, $errfile, $errline){
  34. handle_error(
  35. array(
  36. 'type'=> $errno,
  37. 'message'=> $errstr,
  38. 'file'=> $errfile,
  39. 'line'=> $errline
  40. ),
  41. debug_backtrace(),
  42. get_included_files()
  43. );
  44. },E_ALL);
  45. register_shutdown_function(function(){
  46. $error = error_get_last();
  47. if(!is_null($error) && $error['type'] == 1){
  48. handle_error(
  49. $error,
  50. debug_backtrace(),
  51. get_included_files()
  52. );
  53. }
  54. });
  55. ?>