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
25 * Optional list of errors returned in a JSON body of an HTTP error response.
27 protected $errors = array();
30 * @var array $retryMap Map of errors with retry counts.
32 private $retryMap = array();
35 * Override default constructor to add the ability to set $errors and a retry
38 * @param string $message
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(
48 Exception
$previous = null,
50 array $retryMap = null
52 if (version_compare(PHP_VERSION
, '5.3.0') >= 0) {
53 parent
::__construct($message, $code, $previous);
55 parent
::__construct($message, $code);
58 $this->errors
= $errors;
60 if (is_array($retryMap)) {
61 $this->retryMap
= $retryMap;
66 * An example of the possible errors returned.
70 * "reason": "authError",
71 * "message": "Invalid Credentials",
72 * "locationType": "header",
73 * "location": "Authorization",
76 * @return [{string, string}] List of errors return in an HTTP response or [].
78 public function getErrors()
84 * Gets the number of times the associated task can be retried.
86 * NOTE: -1 is returned if the task can be retried indefinitely
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']];