ApiException.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /*
  3. * Copyright 2016 Google LLC
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Google\ApiCore;
  33. use Exception;
  34. use Google\Protobuf\Internal\RepeatedField;
  35. use Google\Rpc\Status;
  36. use GuzzleHttp\Exception\RequestException;
  37. use Google\ApiCore\Testing\MockStatus;
  38. use stdClass;
  39. /**
  40. * Represents an exception thrown during an RPC.
  41. */
  42. class ApiException extends Exception
  43. {
  44. private $status;
  45. private $metadata;
  46. private $basicMessage;
  47. private $decodedMetadataErrorInfo;
  48. /**
  49. * ApiException constructor.
  50. * @param string $message
  51. * @param int $code
  52. * @param string|null $status
  53. * @param array $optionalArgs {
  54. * @type Exception|null $previous
  55. * @type array|null $metadata
  56. * @type string|null $basicMessage
  57. * }
  58. */
  59. public function __construct(
  60. string $message,
  61. int $code,
  62. string $status = null,
  63. array $optionalArgs = []
  64. ) {
  65. $optionalArgs += [
  66. 'previous' => null,
  67. 'metadata' => null,
  68. 'basicMessage' => $message,
  69. ];
  70. parent::__construct($message, $code, $optionalArgs['previous']);
  71. $this->status = $status;
  72. $this->metadata = $optionalArgs['metadata'];
  73. $this->basicMessage = $optionalArgs['basicMessage'];
  74. if ($this->metadata) {
  75. $this->decodedMetadataErrorInfo = self::decodeMetadataErrorInfo($this->metadata);
  76. }
  77. }
  78. public function getStatus()
  79. {
  80. return $this->status;
  81. }
  82. /**
  83. * Returns null if metadata does not contain error info, or returns containsErrorInfo() array
  84. * if the metadata does contain error info.
  85. * @param array $metadata
  86. * @return array $details {
  87. * @type string|null $reason
  88. * @type string|null $domain
  89. * @type array|null $errorInfoMetadata
  90. * }
  91. */
  92. private static function decodeMetadataErrorInfo(array $metadata)
  93. {
  94. $details = [];
  95. // ApiExceptions created from RPC status have metadata that is an array of objects.
  96. if (is_object(reset($metadata))) {
  97. $metadataRpcStatus = Serializer::decodeAnyMessages($metadata);
  98. $details = self::containsErrorInfo($metadataRpcStatus);
  99. } elseif (self::containsErrorInfo($metadata)) {
  100. $details = self::containsErrorInfo($metadata);
  101. } else {
  102. // For GRPC-based responses, the $metadata needs to be decoded.
  103. $metadataGrpc = Serializer::decodeMetadata($metadata);
  104. $details = self::containsErrorInfo($metadataGrpc);
  105. }
  106. return $details;
  107. }
  108. /**
  109. * Returns the `reason` in ErrorInfo for an exception, or null if there is no ErrorInfo.
  110. * @return string|null $reason
  111. */
  112. public function getReason()
  113. {
  114. return ($this->decodedMetadataErrorInfo) ? $this->decodedMetadataErrorInfo['reason'] : null;
  115. }
  116. /**
  117. * Returns the `domain` in ErrorInfo for an exception, or null if there is no ErrorInfo.
  118. * @return string|null $domain
  119. */
  120. public function getDomain()
  121. {
  122. return ($this->decodedMetadataErrorInfo) ? $this->decodedMetadataErrorInfo['domain'] : null;
  123. }
  124. /**
  125. * Returns the `metadata` in ErrorInfo for an exception, or null if there is no ErrorInfo.
  126. * @return array|null $errorInfoMetadata
  127. */
  128. public function getErrorInfoMetadata()
  129. {
  130. return ($this->decodedMetadataErrorInfo) ? $this->decodedMetadataErrorInfo['errorInfoMetadata'] : null;
  131. }
  132. /**
  133. * @param stdClass $status
  134. * @return ApiException
  135. */
  136. public static function createFromStdClass(stdClass $status)
  137. {
  138. $metadata = property_exists($status, 'metadata') ? $status->metadata : null;
  139. return self::create(
  140. $status->details,
  141. $status->code,
  142. $metadata,
  143. Serializer::decodeMetadata((array) $metadata)
  144. );
  145. }
  146. /**
  147. * @param string $basicMessage
  148. * @param int $rpcCode
  149. * @param array|null $metadata
  150. * @param Exception $previous
  151. * @return ApiException
  152. */
  153. public static function createFromApiResponse(
  154. $basicMessage,
  155. $rpcCode,
  156. array $metadata = null,
  157. Exception $previous = null
  158. ) {
  159. return self::create(
  160. $basicMessage,
  161. $rpcCode,
  162. $metadata,
  163. Serializer::decodeMetadata((array) $metadata),
  164. $previous
  165. );
  166. }
  167. /**
  168. * For REST-based responses, the metadata does not need to be decoded.
  169. *
  170. * @param string $basicMessage
  171. * @param int $rpcCode
  172. * @param array|null $metadata
  173. * @param Exception $previous
  174. * @return ApiException
  175. */
  176. public static function createFromRestApiResponse(
  177. $basicMessage,
  178. $rpcCode,
  179. array $metadata = null,
  180. Exception $previous = null
  181. ) {
  182. return self::create(
  183. $basicMessage,
  184. $rpcCode,
  185. $metadata,
  186. is_null($metadata) ? [] : $metadata,
  187. $previous
  188. );
  189. }
  190. /**
  191. * Checks if decoded metadata includes errorInfo message.
  192. * If errorInfo is set, it will always contain `reason`, `domain`, and `metadata` keys.
  193. * @param array $decodedMetadata
  194. * @return array {
  195. * @type string $reason
  196. * @type string $domain
  197. * @type array $errorInfoMetadata
  198. * }
  199. */
  200. private static function containsErrorInfo(array $decodedMetadata)
  201. {
  202. if (empty($decodedMetadata)) {
  203. return [];
  204. }
  205. foreach ($decodedMetadata as $value) {
  206. $isErrorInfoArray = isset($value['reason']) && isset($value['domain']) && isset($value['metadata']);
  207. if ($isErrorInfoArray) {
  208. return [
  209. 'reason' => $value['reason'],
  210. 'domain' => $value['domain'],
  211. 'errorInfoMetadata' => $value['metadata'],
  212. ];
  213. }
  214. }
  215. return [];
  216. }
  217. /**
  218. * Construct an ApiException with a useful message, including decoded metadata.
  219. * If the decoded metadata includes an errorInfo message, then the domain, reason,
  220. * and metadata fields from that message are hoisted directly into the error.
  221. *
  222. * @param string $basicMessage
  223. * @param int $rpcCode
  224. * @param iterable|null $metadata
  225. * @param array $decodedMetadata
  226. * @param Exception|null $previous
  227. * @return ApiException
  228. */
  229. private static function create(
  230. string $basicMessage,
  231. int $rpcCode,
  232. $metadata,
  233. array $decodedMetadata,
  234. Exception $previous = null
  235. ) {
  236. $containsErrorInfo = self::containsErrorInfo($decodedMetadata);
  237. $rpcStatus = ApiStatus::statusFromRpcCode($rpcCode);
  238. $messageData = [
  239. 'message' => $basicMessage,
  240. 'code' => $rpcCode,
  241. 'status' => $rpcStatus,
  242. 'details' => $decodedMetadata
  243. ];
  244. if ($containsErrorInfo) {
  245. $messageData = array_merge($containsErrorInfo, $messageData);
  246. }
  247. $message = json_encode($messageData, JSON_PRETTY_PRINT);
  248. if ($metadata instanceof RepeatedField) {
  249. $metadata = iterator_to_array($metadata);
  250. }
  251. return new ApiException($message, $rpcCode, $rpcStatus, [
  252. 'previous' => $previous,
  253. 'metadata' => $metadata,
  254. 'basicMessage' => $basicMessage,
  255. ]);
  256. }
  257. /**
  258. * @param Status $status
  259. * @return ApiException
  260. */
  261. public static function createFromRpcStatus(Status $status)
  262. {
  263. return self::create(
  264. $status->getMessage(),
  265. $status->getCode(),
  266. $status->getDetails(),
  267. Serializer::decodeAnyMessages($status->getDetails())
  268. );
  269. }
  270. /**
  271. * Creates an ApiException from a GuzzleHttp RequestException.
  272. *
  273. * @param RequestException $ex
  274. * @param boolean $isStream
  275. * @return ApiException
  276. * @throws ValidationException
  277. */
  278. public static function createFromRequestException(RequestException $ex, bool $isStream = false)
  279. {
  280. $res = $ex->getResponse();
  281. $body = (string) $res->getBody();
  282. $decoded = json_decode($body, true);
  283. // A streaming response body will return one error in an array. Parse
  284. // that first (and only) error message, if provided.
  285. if ($isStream && isset($decoded[0])) {
  286. $decoded = $decoded[0];
  287. }
  288. if (isset($decoded['error']) && $decoded['error']) {
  289. $error = $decoded['error'];
  290. $basicMessage = $error['message'] ?? '';
  291. $code = isset($error['status'])
  292. ? ApiStatus::rpcCodeFromStatus($error['status'])
  293. : $ex->getCode();
  294. $metadata = $error['details'] ?? null;
  295. return static::createFromRestApiResponse($basicMessage, $code, $metadata);
  296. }
  297. // Use the RPC code instead of the HTTP Status Code.
  298. $code = ApiStatus::rpcCodeFromHttpStatusCode($res->getStatusCode());
  299. return static::createFromApiResponse($body, $code);
  300. }
  301. /**
  302. * @return null|string
  303. */
  304. public function getBasicMessage()
  305. {
  306. return $this->basicMessage;
  307. }
  308. /**
  309. * @return mixed[]
  310. */
  311. public function getMetadata()
  312. {
  313. return $this->metadata;
  314. }
  315. /**
  316. * String representation of ApiException
  317. * @return string
  318. */
  319. public function __toString()
  320. {
  321. return __CLASS__ . ": $this->message\n";
  322. }
  323. }