From 59ce797967a9ecd1bf8984e29212268da29e1de4 Mon Sep 17 00:00:00 2001 From: Levan Velijanashvili Date: Wed, 30 Nov 2022 01:32:15 +0400 Subject: [PATCH] Throw specific exceptions --- src/GoogleTranslate.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index f98c83f..61079ab 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -5,6 +5,10 @@ use ErrorException; use GuzzleHttp\Client; use JsonException; +use Stichoza\GoogleTranslate\Exceptions\LargeTextException; +use Stichoza\GoogleTranslate\Exceptions\RateLimitException; +use Stichoza\GoogleTranslate\Exceptions\TranslationDecodingException; +use Stichoza\GoogleTranslate\Exceptions\TranslationRequestException; use Stichoza\GoogleTranslate\Tokens\GoogleTokenGenerator; use Stichoza\GoogleTranslate\Tokens\TokenProviderInterface; use Throwable; @@ -309,6 +313,12 @@ public function getResponse(string $string): array $response = $this->client->get($this->url, [ 'query' => $queryUrl, ] + $this->options); + } catch (GuzzleException $e) { + match ($e->getCode()) { + 429, 503 => throw new RateLimitException($e->getMessage(), $e->getCode()), + 413 => throw new LargeTextException($e->getMessage(), $e->getCode()), + default => throw new TranslationRequestException($e->getMessage(), $e->getCode()), + }; } catch (Throwable $e) { throw new TranslationRequestException($e->getMessage(), $e->getCode()); }