|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2024 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 | + |
| 33 | +namespace Google\Cloud\Core\Middleware; |
| 34 | + |
| 35 | +use Google\ApiCore\ApiException; |
| 36 | +use Google\ApiCore\BidiStream; |
| 37 | +use Google\ApiCore\Call; |
| 38 | +use Google\ApiCore\ClientStream; |
| 39 | +use Google\ApiCore\Middleware\MiddlewareInterface; |
| 40 | +use Google\ApiCore\ServerStream; |
| 41 | +use Google\Cloud\Core\RequestProcessorTrait; |
| 42 | +use GuzzleHttp\Promise\PromiseInterface; |
| 43 | +use Throwable; |
| 44 | + |
| 45 | +/** |
| 46 | + * Middleware that wraps any Api Exception to a `Google\Cloud\Core\Exception` |
| 47 | + * exception class. This is primarily to maintain backwards compatibility with |
| 48 | + * previous Spanner versions. |
| 49 | + * |
| 50 | + * @internal |
| 51 | + */ |
| 52 | +class ExceptionMiddleware implements MiddlewareInterface |
| 53 | +{ |
| 54 | + use RequestProcessorTrait; |
| 55 | + |
| 56 | + /** @var callable */ |
| 57 | + private $nextHandler; |
| 58 | + |
| 59 | + public function __construct(callable $nextHandler) { |
| 60 | + $this->nextHandler = $nextHandler; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param Call $call |
| 65 | + * @param array $options |
| 66 | + * |
| 67 | + * @return PromiseInterface|ClientStream|ServerStream|BidiStream |
| 68 | + */ |
| 69 | + public function __invoke(Call $call, array $options) |
| 70 | + { |
| 71 | + $response = ($this->nextHandler)($call, $options); |
| 72 | + if ($response instanceof PromiseInterface) { |
| 73 | + return $response->then(null, function ($value) { |
| 74 | + if ($value instanceof \Google\ApiCore\ApiException) { |
| 75 | + throw $this->convertToGoogleException($value); |
| 76 | + } |
| 77 | + if ($value instanceof Throwable) { |
| 78 | + throw $value; |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + // this can also be a Stream |
| 83 | + return $response; |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments