11<?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+ */
217
318require_once realpath (dirname (__FILE__ ) . '/../../../autoload.php ' );
419
5- class Google_Service_Exception extends Google_Exception
20+ class Google_Service_Exception extends Google_Exception implements Google_Task_Retryable
621{
722 /**
823 * Optional list of errors returned in a JSON body of an HTTP error response.
924 */
1025 protected $ errors = array ();
1126
1227 /**
13- * Override default constructor to add ability to set $errors.
28+ * @var array $retryMap Map of errors with retry counts.
29+ */
30+ private $ retryMap = array ();
31+
32+ /**
33+ * Override default constructor to add the ability to set $errors and a retry
34+ * map.
1435 *
1536 * @param string $message
1637 * @param int $code
1738 * @param Exception|null $previous
1839 * @param [{string, string}] errors List of errors returned in an HTTP
1940 * response. Defaults to [].
41+ * @param array|null $retryMap Map of errors with retry counts.
2042 */
2143 public function __construct (
2244 $ message ,
2345 $ code = 0 ,
2446 Exception $ previous = null ,
25- $ errors = array ()
47+ $ errors = array (),
48+ array $ retryMap = null
2649 ) {
2750 if (version_compare (PHP_VERSION , '5.3.0 ' ) >= 0 ) {
2851 parent ::__construct ($ message , $ code , $ previous );
@@ -31,6 +54,10 @@ public function __construct(
3154 }
3255
3356 $ this ->errors = $ errors ;
57+
58+ if (is_array ($ retryMap )) {
59+ $ this ->retryMap = $ retryMap ;
60+ }
3461 }
3562
3663 /**
@@ -50,4 +77,27 @@ public function getErrors()
5077 {
5178 return $ this ->errors ;
5279 }
80+
81+ /**
82+ * Gets the number of times the associated task can be retried.
83+ *
84+ * NOTE: -1 is returned if the task can be retried indefinitely
85+ *
86+ * @return integer
87+ */
88+ public function allowedRetries ()
89+ {
90+ if (isset ($ this ->retryMap [$ this ->code ])) {
91+ return $ this ->retryMap [$ this ->code ];
92+ }
93+
94+ $ errors = $ this ->getErrors ();
95+
96+ if (!empty ($ errors ) && isset ($ errors [0 ]['reason ' ]) &&
97+ isset ($ this ->retryMap [$ errors [0 ]['reason ' ]])) {
98+ return $ this ->retryMap [$ errors [0 ]['reason ' ]];
99+ }
100+
101+ return 0 ;
102+ }
53103}
0 commit comments