Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @author   
   9   * @category Horde
  10   * @license  http://www.horde.org/licenses/lgpl21 LGPL
  11   * @package  Exception
  12   */
  13  
  14  /**
  15   * Horde exception class that converts PEAR errors to exceptions.
  16   *
  17   * @author    
  18   * @category  Horde
  19   * @copyright 2008-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL
  21   * @package   Exception
  22   */
  23  class Horde_Exception_Pear extends Horde_Exception
  24  {
  25      /**
  26       * The class name for generated exceptions.
  27       *
  28       * @var string
  29       */
  30      protected static $_class = __CLASS__;
  31  
  32      /**
  33       * Exception constructor.
  34       *
  35       * @param PEAR_Error $error The PEAR error.
  36       */
  37      public function __construct(PEAR_Error $error)
  38      {
  39          parent::__construct($error->getMessage(), $error->getCode());
  40          $this->details = $this->_getPearTrace($error);
  41      }
  42  
  43      /**
  44       * Return a trace for the PEAR error.
  45       *
  46       * @param PEAR_Error $error The PEAR error.
  47       *
  48       * @return string The backtrace as a string.
  49       */
  50      private function _getPearTrace(PEAR_Error $error)
  51      {
  52          $pear_error = '';
  53          $backtrace = $error->getBacktrace();
  54          if (!empty($backtrace)) {
  55              $pear_error .= 'PEAR backtrace:' . "\n\n";
  56              foreach ($backtrace as $frame) {
  57                  $pear_error .=
  58                        (isset($frame['class']) ? $frame['class'] : '')
  59                      . (isset($frame['type']) ? $frame['type'] : '')
  60                      . (isset($frame['function']) ? $frame['function'] : 'unkown') . ' '
  61                      . (isset($frame['file']) ? $frame['file'] : 'unkown') . ':'
  62                      . (isset($frame['line']) ? $frame['line'] : 'unkown') . "\n";
  63              }
  64          }
  65          $userinfo = $error->getUserInfo();
  66          if (!empty($userinfo)) {
  67              $pear_error .= "\n" . 'PEAR user info:' . "\n\n";
  68              if (is_string($userinfo)) {
  69                  $pear_error .= $userinfo;
  70              } else {
  71                  $pear_error .= print_r($userinfo, true);
  72              }
  73          }
  74          return $pear_error;
  75      }
  76  
  77      /**
  78       * Exception handling.
  79       *
  80       * @param mixed $result The result to be checked for a PEAR_Error.
  81       *
  82       * @return mixed Returns the original result if it was no PEAR_Error.
  83       *
  84       * @throws Horde_Exception_Pear In case the result was a PEAR_Error.
  85       */
  86      public static function catchError($result)
  87      {
  88          if ($result instanceof PEAR_Error) {
  89              throw new self::$_class($result);
  90          }
  91          return $result;
  92      }
  93  }