MDL-51531 libraries: Upgrade google libraries to 1.1.5
[moodle.git] / lib / google / src / Google / Service / Exception.php
blob65c945b73d14aa8ee5adf76e2bf4fc203cfa3254
1 <?php
2 /*
3 * Copyright 2014 Google Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 if (!class_exists('Google_Client')) {
19 require_once dirname(__FILE__) . '/../autoload.php';
22 class Google_Service_Exception extends Google_Exception implements Google_Task_Retryable
24 /**
25 * Optional list of errors returned in a JSON body of an HTTP error response.
27 protected $errors = array();
29 /**
30 * @var array $retryMap Map of errors with retry counts.
32 private $retryMap = array();
34 /**
35 * Override default constructor to add the ability to set $errors and a retry
36 * map.
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.
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);
58 $this->errors = $errors;
60 if (is_array($retryMap)) {
61 $this->retryMap = $retryMap;
65 /**
66 * An example of the possible errors returned.
68 * {
69 * "domain": "global",
70 * "reason": "authError",
71 * "message": "Invalid Credentials",
72 * "locationType": "header",
73 * "location": "Authorization",
74 * }
76 * @return [{string, string}] List of errors return in an HTTP response or [].
78 public function getErrors()
80 return $this->errors;
83 /**
84 * Gets the number of times the associated task can be retried.
86 * NOTE: -1 is returned if the task can be retried indefinitely
88 * @return integer
90 public function allowedRetries()
92 if (isset($this->retryMap[$this->code])) {
93 return $this->retryMap[$this->code];
96 $errors = $this->getErrors();
98 if (!empty($errors) && isset($errors[0]['reason']) &&
99 isset($this->retryMap[$errors[0]['reason']])) {
100 return $this->retryMap[$errors[0]['reason']];
103 return 0;