/google_api/Service/Exception.php

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