Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  /*
   3   * Copyright 2014 Google Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *     http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  if (!class_exists('Google_Client')) {
  19    require_once dirname(__FILE__) . '/../autoload.php';
  20  }
  21  
  22  #[AllowDynamicProperties]
  23  class Google_Service_Exception extends Google_Exception implements Google_Task_Retryable
  24  {
  25    /**
  26     * Optional list of errors returned in a JSON body of an HTTP error response.
  27     */
  28    protected $errors = array();
  29  
  30    /**
  31     * @var array $retryMap Map of errors with retry counts.
  32     */
  33    private $retryMap = array();
  34  
  35    /**
  36     * Override default constructor to add the ability to set $errors and a retry
  37     * map.
  38     *
  39     * @param string $message
  40     * @param int $code
  41     * @param Exception|null $previous
  42     * @param [{string, string}] errors List of errors returned in an HTTP
  43     * response.  Defaults to [].
  44     * @param array|null $retryMap Map of errors with retry counts.
  45     */
  46    public function __construct(
  47        $message,
  48        $code = 0,
  49        Exception $previous = null,
  50        $errors = array(),
  51        array $retryMap = null
  52    ) {
  53      if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  54        parent::__construct($message, $code, $previous);
  55      } else {
  56        parent::__construct($message, $code);
  57      }
  58  
  59      $this->errors = $errors;
  60  
  61      if (is_array($retryMap)) {
  62        $this->retryMap = $retryMap;
  63      }
  64    }
  65  
  66    /**
  67     * An example of the possible errors returned.
  68     *
  69     * {
  70     *   "domain": "global",
  71     *   "reason": "authError",
  72     *   "message": "Invalid Credentials",
  73     *   "locationType": "header",
  74     *   "location": "Authorization",
  75     * }
  76     *
  77     * @return [{string, string}] List of errors return in an HTTP response or [].
  78     */
  79    public function getErrors()
  80    {
  81      return $this->errors;
  82    }
  83  
  84    /**
  85     * Gets the number of times the associated task can be retried.
  86     *
  87     * NOTE: -1 is returned if the task can be retried indefinitely
  88     *
  89     * @return integer
  90     */
  91    public function allowedRetries()
  92    {
  93      if (isset($this->retryMap[$this->code])) {
  94        return $this->retryMap[$this->code];
  95      }
  96  
  97      $errors = $this->getErrors();
  98  
  99      if (!empty($errors) && isset($errors[0]['reason']) &&
 100          isset($this->retryMap[$errors[0]['reason']])) {
 101        return $this->retryMap[$errors[0]['reason']];
 102      }
 103  
 104      return 0;
 105    }
 106  }