Skip to content

refactor: use lib specific exceptions instead of native ones #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Bridge/HuggingFace/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpLlm\LlmChain\Bridge\HuggingFace;

use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Model\Message\Content\Audio;
use PhpLlm\LlmChain\Model\Message\Content\Image;
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
Expand Down Expand Up @@ -99,6 +100,6 @@ private function getPayload(object|array|string $input, array $options): array
return $payload;
}

throw new \InvalidArgumentException('Unsupported input type: '.get_debug_type($input));
throw new InvalidArgumentException('Unsupported input type: '.get_debug_type($input));
}
}
12 changes: 7 additions & 5 deletions src/Bridge/HuggingFace/ResponseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PhpLlm\LlmChain\Bridge\HuggingFace\Output\TokenClassificationResult;
use PhpLlm\LlmChain\Bridge\HuggingFace\Output\ZeroShotClassificationResult;
use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Model\Model as BaseModel;
use PhpLlm\LlmChain\Model\Response\BinaryResponse;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
Expand All @@ -33,11 +35,11 @@ public function supports(BaseModel $model, array|string|object $input): bool
public function convert(ResponseInterface $response, array $options = []): LlmResponse
{
if (503 === $response->getStatusCode()) {
return throw new \RuntimeException('Service unavailable.');
return throw new RuntimeException('Service unavailable.');
}

if (404 === $response->getStatusCode()) {
return throw new \InvalidArgumentException('Model, provider or task not found (404).');
return throw new InvalidArgumentException('Model, provider or task not found (404).');
}

$headers = $response->getHeaders(false);
Expand All @@ -48,11 +50,11 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
$message = is_string($content) ? $content :
(is_array($content['error']) ? $content['error'][0] : $content['error']);

throw new \InvalidArgumentException(sprintf('API Client Error (%d): %s', $response->getStatusCode(), $message));
throw new InvalidArgumentException(sprintf('API Client Error (%d): %s', $response->getStatusCode(), $message));
}

if (200 !== $response->getStatusCode()) {
throw new \RuntimeException('Unhandled response code: '.$response->getStatusCode());
throw new RuntimeException('Unhandled response code: '.$response->getStatusCode());
}

$task = $options['task'] ?? null;
Expand All @@ -78,7 +80,7 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
Task::TRANSLATION => new TextResponse($content[0]['translation_text'] ?? ''),
Task::ZERO_SHOT_CLASSIFICATION => new StructuredResponse(ZeroShotClassificationResult::fromArray($content)),

default => throw new \RuntimeException(sprintf('Unsupported task: %s', $task)),
default => throw new RuntimeException(sprintf('Unsupported task: %s', $task)),
};
}
}
3 changes: 2 additions & 1 deletion src/Bridge/OpenAI/DallE/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE;

use PhpLlm\LlmChain\Bridge\OpenAI\DallE;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Platform\ModelClient as PlatformResponseFactory;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
{
$response = $response->toArray();
if (!isset($response['data'][0])) {
throw new \RuntimeException('No image generated.');
throw new RuntimeException('No image generated.');
}

$images = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Bridge/TransformersPHP/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;

use Codewithkyrian\Transformers\Pipelines\Task;
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Model\Model as BaseModel;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Model\Response\StructuredResponse;
Expand All @@ -25,7 +26,7 @@ public function supports(BaseModel $model, object|array|string $input): bool
public function request(BaseModel $model, object|array|string $input, array $options = []): ResponseInterface
{
if (!isset($options['task'])) {
throw new \InvalidArgumentException('The task option is required.');
throw new InvalidArgumentException('The task option is required.');
}

$pipeline = pipeline(
Expand All @@ -44,7 +45,7 @@ public function request(BaseModel $model, object|array|string $input, array $opt
public function convert(ResponseInterface $response, array $options = []): LlmResponse
{
if (!$response instanceof PipelineResponse) {
throw new \InvalidArgumentException('The response is not a valid TransformersPHP response.');
throw new InvalidArgumentException('The response is not a valid TransformersPHP response.');
}

$task = $options['task'];
Expand Down
7 changes: 4 additions & 3 deletions src/Bridge/TransformersPHP/PipelineResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;

use Codewithkyrian\Transformers\Pipelines\Pipeline;
use PhpLlm\LlmChain\Exception\RuntimeException;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class PipelineResponse implements ResponseInterface
Expand Down Expand Up @@ -35,7 +36,7 @@ public function getHeaders(bool $throw = true): array

public function getContent(bool $throw = true): string
{
throw new \RuntimeException('Not implemented');
throw new RuntimeException('Not implemented');
}

/**
Expand All @@ -48,11 +49,11 @@ public function toArray(bool $throw = true): array

public function cancel(): void
{
throw new \RuntimeException('Not implemented');
throw new RuntimeException('Not implemented');
}

public function getInfo(?string $type = null): mixed
{
throw new \RuntimeException('Not implemented');
throw new RuntimeException('Not implemented');
}
}
3 changes: 2 additions & 1 deletion src/Bridge/TransformersPHP/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;

use Codewithkyrian\Transformers\Transformers;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Platform;

final readonly class PlatformFactory
{
public static function create(): Platform
{
if (!class_exists(Transformers::class)) {
throw new \RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".');
throw new RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".');
}

return new Platform([$handler = new Handler()], [$handler]);
Expand Down
3 changes: 2 additions & 1 deletion src/Chain/JsonSchema/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Chain\JsonSchema;

use PhpLlm\LlmChain\Chain\JsonSchema\Attribute\With;
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
Expand Down Expand Up @@ -155,7 +156,7 @@ private function getTypeSchema(Type $type): array

case $type->isIdentifiedBy(TypeIdentifier::OBJECT):
if ($type instanceof BuiltinType) {
throw new \InvalidArgumentException('Cannot build schema from plain object type.');
throw new InvalidArgumentException('Cannot build schema from plain object type.');
}
assert($type instanceof ObjectType);
if (in_array($type->getClassName(), ['DateTime', 'DateTimeImmutable', 'DateTimeInterface'], true)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Chain/Toolbox/Tool/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Chain\Toolbox\Tool;

use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
use PhpLlm\LlmChain\Exception\RuntimeException;
use Symfony\Component\DomCrawler\Crawler as DomCrawler;
use Symfony\Contracts\HttpClient\HttpClientInterface;

Expand All @@ -15,7 +16,7 @@ public function __construct(
private HttpClientInterface $httpClient,
) {
if (!class_exists(DomCrawler::class)) {
throw new \RuntimeException('The DomCrawler component is not installed. Please install it using "composer require symfony/dom-crawler".');
throw new RuntimeException('The DomCrawler component is not installed. Please install it using "composer require symfony/dom-crawler".');
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Model/Message/Content/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Model\Message\Content;

use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Exception\RuntimeException;

use function Symfony\Component\String\u;

Expand Down Expand Up @@ -32,7 +33,7 @@ public static function fromDataUrl(string $dataUrl): static
public static function fromFile(string $path): static
{
if (!is_readable($path)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable.', $path));
throw new InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable.', $path));
}

return new static(
Expand Down Expand Up @@ -68,7 +69,7 @@ public function asDataUrl(): string
public function asResource()
{
if (null === $this->path) {
throw new \RuntimeException('You can only get a resource after creating fromFile.');
throw new RuntimeException('You can only get a resource after creating fromFile.');
}

return fopen($this->path, 'r');
Expand Down
4 changes: 3 additions & 1 deletion src/Model/Response/BinaryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpLlm\LlmChain\Model\Response;

use PhpLlm\LlmChain\Exception\RuntimeException;

final class BinaryResponse extends BaseResponse
{
public function __construct(
Expand All @@ -25,7 +27,7 @@ public function toBase64(): string
public function toDataUri(): string
{
if (null === $this->mimeType) {
throw new \RuntimeException('Mime type is not set.');
throw new RuntimeException('Mime type is not set.');
}

return 'data:'.$this->mimeType.';base64,'.$this->toBase64();
Expand Down