Pear.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Horde exception class that converts PEAR errors to exceptions.
  4. *
  5. * Copyright 2008-2013 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @category Horde
  11. * @package Exception
  12. */
  13. class Horde_Exception_Pear extends Horde_Exception
  14. {
  15. /**
  16. * The class name for generated exceptions.
  17. *
  18. * @var string
  19. */
  20. static protected $_class = __CLASS__;
  21. /**
  22. * Exception constructor.
  23. *
  24. * @param PEAR_Error $error The PEAR error.
  25. */
  26. public function __construct(PEAR_Error $error)
  27. {
  28. parent::__construct($error->getMessage(), $error->getCode());
  29. $this->details = $this->_getPearTrace($error);
  30. }
  31. /**
  32. * Return a trace for the PEAR error.
  33. *
  34. * @param PEAR_Error $error The PEAR error.
  35. *
  36. * @return string The backtrace as a string.
  37. */
  38. private function _getPearTrace(PEAR_Error $error)
  39. {
  40. $pear_error = '';
  41. $backtrace = $error->getBacktrace();
  42. if (!empty($backtrace)) {
  43. $pear_error .= 'PEAR backtrace:' . "\n\n";
  44. foreach ($backtrace as $frame) {
  45. $pear_error .=
  46. (isset($frame['class']) ? $frame['class'] : '')
  47. . (isset($frame['type']) ? $frame['type'] : '')
  48. . (isset($frame['function']) ? $frame['function'] : 'unkown') . ' '
  49. . (isset($frame['file']) ? $frame['file'] : 'unkown') . ':'
  50. . (isset($frame['line']) ? $frame['line'] : 'unkown') . "\n";
  51. }
  52. }
  53. $userinfo = $error->getUserInfo();
  54. if (!empty($userinfo)) {
  55. $pear_error .= "\n" . 'PEAR user info:' . "\n\n";
  56. if (is_string($userinfo)) {
  57. $pear_error .= $userinfo;
  58. } else {
  59. $pear_error .= print_r($userinfo, true);
  60. }
  61. }
  62. return $pear_error;
  63. }
  64. /**
  65. * Exception handling.
  66. *
  67. * @param mixed $result The result to be checked for a PEAR_Error.
  68. *
  69. * @return mixed Returns the original result if it was no PEAR_Error.
  70. *
  71. * @throws Horde_Exception_Pear In case the result was a PEAR_Error.
  72. */
  73. static public function catchError($result)
  74. {
  75. if ($result instanceOf PEAR_Error) {
  76. throw new self::$_class($result);
  77. }
  78. return $result;
  79. }
  80. }