1: <?php
2: /**
3: * Horde exception class that converts PEAR errors to exceptions.
4: *
5: * Copyright 2008-2012 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: /**
23: * Exception constructor.
24: *
25: * @param PEAR_Error $error The PEAR error.
26: */
27: public function __construct(PEAR_Error $error)
28: {
29: parent::__construct($error->getMessage(), $error->getCode());
30: $this->details = $this->_getPearTrace($error);
31: }
32:
33: /**
34: * Return a trace for the PEAR error.
35: *
36: * @param PEAR_Error $error The PEAR error.
37: *
38: * @return string The backtrace as a string.
39: */
40: private function _getPearTrace(PEAR_Error $error)
41: {
42: $pear_error = '';
43: $backtrace = $error->getBacktrace();
44: if (!empty($backtrace)) {
45: $pear_error .= 'PEAR backtrace:' . "\n\n";
46: foreach ($backtrace as $frame) {
47: $pear_error .=
48: (isset($frame['class']) ? $frame['class'] : '')
49: . (isset($frame['type']) ? $frame['type'] : '')
50: . (isset($frame['function']) ? $frame['function'] : 'unkown') . ' '
51: . (isset($frame['file']) ? $frame['file'] : 'unkown') . ':'
52: . (isset($frame['line']) ? $frame['line'] : 'unkown') . "\n";
53: }
54: }
55: $userinfo = $error->getUserInfo();
56: if (!empty($userinfo)) {
57: $pear_error .= "\n" . 'PEAR user info:' . "\n\n";
58: if (is_string($userinfo)) {
59: $pear_error .= $userinfo;
60: } else {
61: $pear_error .= print_r($userinfo, true);
62: }
63: }
64: return $pear_error;
65: }
66:
67: /**
68: * Exception handling.
69: *
70: * @param mixed $result The result to be checked for a PEAR_Error.
71: *
72: * @return mixed Returns the original result if it was no PEAR_Error.
73: *
74: * @throws Horde_Exception_Pear In case the result was a PEAR_Error.
75: */
76: static public function catchError($result)
77: {
78: if ($result instanceOf PEAR_Error) {
79: throw new self::$_class($result);
80: }
81: return $result;
82: }
83: }
84: