error-handler.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. ini_set('display_errors', false);
  3. // set to the user defined error handler
  4. set_error_handler(function($errno, $errstr, $errfile, $errline) {
  5. if (!(error_reporting() & $errno)) {
  6. // This error code is not included in error_reporting, so let it fall
  7. // through to the standard PHP error handler
  8. return false;
  9. }
  10. switch ($errno) {
  11. case E_USER_ERROR:
  12. echo <<<ERROR
  13. <article class="message is-danger">
  14. <div class="message-body">
  15. <strong>Error!!!</strong><br />\n
  16. $errstr<br />\n
  17. Fatal error on line $errline in file $errfile<br />\n
  18. Aborting...<br />\n
  19. </div>
  20. </article>
  21. ERROR;
  22. exit(1);
  23. break;
  24. case E_USER_WARNING:
  25. echo <<<WARNING
  26. <article class="message is-warning">
  27. <div class="message-body">
  28. <strong>Warning!!!</strong><br />\n
  29. $errstr<br />\n
  30. Warning on line $errline in file $errfile<br />\n
  31. </div>
  32. </article>
  33. WARNING;
  34. break;
  35. case E_USER_NOTICE:
  36. echo <<<NOTICE
  37. <article class="message is-dark">
  38. <div class="message-body">
  39. <strong>Notice!!!</strong><br />\n
  40. $errstr<br />\n
  41. Notice on line $errline in file $errfile<br />\n
  42. </div>
  43. </article>
  44. NOTICE;
  45. break;
  46. default:
  47. echo "Unknown error type: [$errno] $errstr<br />\n";
  48. break;
  49. }
  50. /* Don't execute PHP internal error handler */
  51. return true;
  52. });
  53. function shutdown()
  54. {
  55. // This is our shutdown function, in
  56. // here we can do any last operations
  57. // before the script is complete.
  58. echo 'Script executed with success', PHP_EOL;
  59. }
  60. register_shutdown_function(function() {
  61. $error = error_get_last();
  62. switch ($error['type']) {
  63. case E_ERROR:
  64. case E_CORE_ERROR:
  65. case E_COMPILE_ERROR:
  66. case E_CORE_WARNING:
  67. case E_PARSE:
  68. echo <<<ERROR
  69. <article class="message is-danger">
  70. <div class="message-body">
  71. <strong>Error!!!</strong><br />\n
  72. {$error['message']}<br />\n
  73. PHP Internal error on line {$error['line']} in file {$error['file']}<br />\n
  74. Aborting...<br />\n
  75. </div>
  76. </article>
  77. ERROR;
  78. break;
  79. default:
  80. if (!empty($error['type'])) {
  81. echo "Unknown error type: {$error['type']} {$error['message']}<br />\n";
  82. }
  83. }
  84. return true;
  85. });