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.

Differences Between: [Versions 401 and 402] [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  class Google_Service_Exception extends Google_Exception implements Google_Task_Retryable
  23  {
  24    /**
  25     * Optional list of errors returned in a JSON body of an HTTP error response.
  26     */
  27    protected $errors = array();
  28  
  29    /**
  30     * @var array $retryMap Map of errors with retry counts.
  31     */
  32    private $retryMap = array();
  33  
  34    /**
  35     * Override default constructor to add the ability to set $errors and a retry
  36     * map.
  37     *
  38     * @param string $message
  39     * @param int $code
  40     * @param Exception|null $previous
  41     * @param [{string, string}] errors List of errors returned in an HTTP
  42     * response.  Defaults to [].
  43     * @param array|null $retryMap Map of errors with retry counts.
  44     */
  45    public function __construct(
  46        $message,
  47        $code = 0,
  48        Exception $previous = null,
  49        $errors = array(),
  50        array $retryMap = null
  51    ) {
  52      if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  53        parent::__construct($message, $code, $previous);
  54      } else {
  55        parent::__construct($message, $code);
  56      }
  57  
  58      $this->errors = $errors;
  59  
  60      if (is_array($retryMap)) {
  61        $this->retryMap = $retryMap;
  62      }
  63    }
  64  
  65    /**
  66     * An example of the possible errors returned.
  67     *
  68     * {
  69     *   "domain": "global",
  70     *   "reason": "authError",
  71     *   "message": "Invalid Credentials",
  72     *   "locationType": "header",
  73     *   "location": "Authorization",
  74     * }
  75     *
  76     * @return [{string, string}] List of errors return in an HTTP response or [].
  77     */
  78    public function getErrors()
  79    {
  80      return $this->errors;
  81    }
  82  
  83    /**
  84     * Gets the number of times the associated task can be retried.
  85     *
  86     * NOTE: -1 is returned if the task can be retried indefinitely
  87     *
  88     * @return integer
  89     */
  90    public function allowedRetries()
  91    {
  92      if (isset($this->retryMap[$this->code])) {
  93        return $this->retryMap[$this->code];
  94      }
  95  
  96      $errors = $this->getErrors();
  97  
  98      if (!empty($errors) && isset($errors[0]['reason']) &&
  99          isset($this->retryMap[$errors[0]['reason']])) {
 100        return $this->retryMap[$errors[0]['reason']];
 101      }
 102  
 103      return 0;
 104    }
 105  }