Skip to content

Commit b0ee187

Browse files
authored
refactor: use lib specific exceptions instead of native ones (#293)
1 parent 0d7bac5 commit b0ee187

File tree

10 files changed

+30
-18
lines changed

10 files changed

+30
-18
lines changed

src/Bridge/HuggingFace/ModelClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Bridge\HuggingFace;
66

7+
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
78
use PhpLlm\LlmChain\Model\Message\Content\Audio;
89
use PhpLlm\LlmChain\Model\Message\Content\Image;
910
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
@@ -99,6 +100,6 @@ private function getPayload(object|array|string $input, array $options): array
99100
return $payload;
100101
}
101102

102-
throw new \InvalidArgumentException('Unsupported input type: '.get_debug_type($input));
103+
throw new InvalidArgumentException('Unsupported input type: '.get_debug_type($input));
103104
}
104105
}

src/Bridge/HuggingFace/ResponseConverter.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PhpLlm\LlmChain\Bridge\HuggingFace\Output\TokenClassificationResult;
1515
use PhpLlm\LlmChain\Bridge\HuggingFace\Output\ZeroShotClassificationResult;
1616
use PhpLlm\LlmChain\Document\Vector;
17+
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
18+
use PhpLlm\LlmChain\Exception\RuntimeException;
1719
use PhpLlm\LlmChain\Model\Model as BaseModel;
1820
use PhpLlm\LlmChain\Model\Response\BinaryResponse;
1921
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
@@ -33,11 +35,11 @@ public function supports(BaseModel $model, array|string|object $input): bool
3335
public function convert(ResponseInterface $response, array $options = []): LlmResponse
3436
{
3537
if (503 === $response->getStatusCode()) {
36-
return throw new \RuntimeException('Service unavailable.');
38+
return throw new RuntimeException('Service unavailable.');
3739
}
3840

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

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

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

5456
if (200 !== $response->getStatusCode()) {
55-
throw new \RuntimeException('Unhandled response code: '.$response->getStatusCode());
57+
throw new RuntimeException('Unhandled response code: '.$response->getStatusCode());
5658
}
5759

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

81-
default => throw new \RuntimeException(sprintf('Unsupported task: %s', $task)),
83+
default => throw new RuntimeException(sprintf('Unsupported task: %s', $task)),
8284
};
8385
}
8486
}

src/Bridge/OpenAI/DallE/ModelClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE;
66

77
use PhpLlm\LlmChain\Bridge\OpenAI\DallE;
8+
use PhpLlm\LlmChain\Exception\RuntimeException;
89
use PhpLlm\LlmChain\Model\Model;
910
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
1011
use PhpLlm\LlmChain\Platform\ModelClient as PlatformResponseFactory;
@@ -47,7 +48,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
4748
{
4849
$response = $response->toArray();
4950
if (!isset($response['data'][0])) {
50-
throw new \RuntimeException('No image generated.');
51+
throw new RuntimeException('No image generated.');
5152
}
5253

5354
$images = [];

src/Bridge/TransformersPHP/Handler.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;
66

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

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

5051
$task = $options['task'];

src/Bridge/TransformersPHP/PipelineResponse.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;
66

77
use Codewithkyrian\Transformers\Pipelines\Pipeline;
8+
use PhpLlm\LlmChain\Exception\RuntimeException;
89
use Symfony\Contracts\HttpClient\ResponseInterface;
910

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

3637
public function getContent(bool $throw = true): string
3738
{
38-
throw new \RuntimeException('Not implemented');
39+
throw new RuntimeException('Not implemented');
3940
}
4041

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

4950
public function cancel(): void
5051
{
51-
throw new \RuntimeException('Not implemented');
52+
throw new RuntimeException('Not implemented');
5253
}
5354

5455
public function getInfo(?string $type = null): mixed
5556
{
56-
throw new \RuntimeException('Not implemented');
57+
throw new RuntimeException('Not implemented');
5758
}
5859
}

src/Bridge/TransformersPHP/PlatformFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;
66

77
use Codewithkyrian\Transformers\Transformers;
8+
use PhpLlm\LlmChain\Exception\RuntimeException;
89
use PhpLlm\LlmChain\Platform;
910

1011
final readonly class PlatformFactory
1112
{
1213
public static function create(): Platform
1314
{
1415
if (!class_exists(Transformers::class)) {
15-
throw new \RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".');
16+
throw new RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".');
1617
}
1718

1819
return new Platform([$handler = new Handler()], [$handler]);

src/Chain/JsonSchema/Factory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Chain\JsonSchema;
66

77
use PhpLlm\LlmChain\Chain\JsonSchema\Attribute\With;
8+
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
89
use Symfony\Component\TypeInfo\Type;
910
use Symfony\Component\TypeInfo\Type\BuiltinType;
1011
use Symfony\Component\TypeInfo\Type\CollectionType;
@@ -155,7 +156,7 @@ private function getTypeSchema(Type $type): array
155156

156157
case $type->isIdentifiedBy(TypeIdentifier::OBJECT):
157158
if ($type instanceof BuiltinType) {
158-
throw new \InvalidArgumentException('Cannot build schema from plain object type.');
159+
throw new InvalidArgumentException('Cannot build schema from plain object type.');
159160
}
160161
assert($type instanceof ObjectType);
161162
if (in_array($type->getClassName(), ['DateTime', 'DateTimeImmutable', 'DateTimeInterface'], true)) {

src/Chain/Toolbox/Tool/Crawler.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Chain\Toolbox\Tool;
66

77
use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
8+
use PhpLlm\LlmChain\Exception\RuntimeException;
89
use Symfony\Component\DomCrawler\Crawler as DomCrawler;
910
use Symfony\Contracts\HttpClient\HttpClientInterface;
1011

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

src/Model/Message/Content/File.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Model\Message\Content;
66

77
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
8+
use PhpLlm\LlmChain\Exception\RuntimeException;
89

910
use function Symfony\Component\String\u;
1011

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

3839
return new static(
@@ -68,7 +69,7 @@ public function asDataUrl(): string
6869
public function asResource()
6970
{
7071
if (null === $this->path) {
71-
throw new \RuntimeException('You can only get a resource after creating fromFile.');
72+
throw new RuntimeException('You can only get a resource after creating fromFile.');
7273
}
7374

7475
return fopen($this->path, 'r');

src/Model/Response/BinaryResponse.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpLlm\LlmChain\Model\Response;
66

7+
use PhpLlm\LlmChain\Exception\RuntimeException;
8+
79
final class BinaryResponse extends BaseResponse
810
{
911
public function __construct(
@@ -25,7 +27,7 @@ public function toBase64(): string
2527
public function toDataUri(): string
2628
{
2729
if (null === $this->mimeType) {
28-
throw new \RuntimeException('Mime type is not set.');
30+
throw new RuntimeException('Mime type is not set.');
2931
}
3032

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

0 commit comments

Comments
 (0)