Skip to content

Commit

Permalink
Throw specific exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stichoza committed Nov 29, 2022
1 parent 6f6e57f commit 59ce797
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 59ce797

Please sign in to comment.