AmfphpErrorHandler.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. */
  10. /**
  11. * sets a custom error handler to catch notices and such and transform them to exceptions.
  12. *
  13. *
  14. * @todo this could be enhanced to use filters so that at the end of the gateway execution the error handling is set back to normal. This could be useful especially for integration with frameworks.
  15. * @package Amfphp_Plugins_ErrorHandler
  16. * @author Ariel Sommeria-Klein
  17. */
  18. class AmfphpErrorHandler {
  19. /**
  20. * constructor. Add filters on the HookManager.
  21. * @param array $config optional key/value pairs in an associative array. Used to override default configuration values.
  22. */
  23. public function __construct(array $config = null) {
  24. set_error_handler('custom_warning_handler');
  25. }
  26. }
  27. /**
  28. * Only throw an exception if the error level would report it
  29. * @param int $errno
  30. * @param string $errstr
  31. * @param string $errfile
  32. * @param int $errline
  33. * @param mixed $errcontext
  34. * @throws Exception
  35. */
  36. function custom_warning_handler($errno, $errstr, $errfile, $errline, $errcontext) {
  37. if ($errno & error_reporting()) {
  38. throw new Amfphp_Core_Exception("$errstr . \n<br>file: $errfile \n<br>line: $errline \n<br>context: " . print_r($errcontext, true), $errno);
  39. }
  40. }
  41. ?>