Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 7da2538

Browse files
committed
Use package-specific exceptions instead of generic PHP exceptions
1 parent 8f2f5d3 commit 7da2538

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Platform\Exception;
6+
7+
class LogicException extends \LogicException implements ExceptionInterface
8+
{
9+
}

src/Platform/Fabric/FabricInputProcessor.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use PhpLlm\FabricPattern\Pattern;
88
use PhpLlm\LlmChain\Chain\Input;
99
use PhpLlm\LlmChain\Chain\InputProcessorInterface;
10+
use PhpLlm\LlmChain\Platform\Exception\InvalidArgumentException;
11+
use PhpLlm\LlmChain\Platform\Exception\LogicException;
12+
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
1013
use PhpLlm\LlmChain\Platform\Message\SystemMessage;
1114

1215
/**
1316
* Requires the "php-llm/fabric-pattern" package to be installed.
14-
*
15-
* This processor allows adding Fabric patterns through options:
16-
* - fabric_pattern: string - The pattern name to load
1717
*/
1818
final readonly class FabricInputProcessor implements InputProcessorInterface
1919
{
@@ -27,27 +27,22 @@ public function processInput(Input $input): void
2727

2828
$pattern = $options['fabric_pattern'];
2929
if (!\is_string($pattern)) {
30-
throw new \InvalidArgumentException('The "fabric_pattern" option must be a string');
30+
throw new InvalidArgumentException('The "fabric_pattern" option must be a string');
3131
}
3232

33-
// Check if there's already a system message
3433
if (null !== $input->messages->getSystemMessage()) {
35-
throw new \LogicException('Cannot add Fabric pattern: MessageBag already contains a system message');
34+
throw new LogicException('Cannot add Fabric pattern: MessageBag already contains a system message');
3635
}
3736

38-
// Check if fabric-pattern package is installed
3937
if (!class_exists(Pattern::class)) {
40-
throw new \RuntimeException('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package: composer require php-llm/fabric-pattern');
38+
throw new RuntimeException('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package: composer require php-llm/fabric-pattern');
4139
}
4240

43-
// Load the pattern and prepend as system message
4441
$content = (new Pattern())->load($pattern);
4542
$systemMessage = new SystemMessage($content);
4643

47-
// Prepend the system message
4844
$input->messages = $input->messages->prepend($systemMessage);
4945

50-
// Remove the fabric option from the chain options
5146
unset($options['fabric_pattern']);
5247
$input->setOptions($options);
5348
}

src/Platform/Message/Message.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Platform\Message;
66

77
use PhpLlm\FabricPattern\Pattern;
8+
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
89
use PhpLlm\LlmChain\Platform\Message\Content\ContentInterface;
910
use PhpLlm\LlmChain\Platform\Message\Content\Text;
1011
use PhpLlm\LlmChain\Platform\Response\ToolCall;
@@ -38,7 +39,7 @@ public static function forSystem(\Stringable|string $content): SystemMessage
3839
public static function fabric(string $pattern): SystemMessage
3940
{
4041
if (!class_exists(Pattern::class)) {
41-
throw new \RuntimeException('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package: composer require php-llm/fabric-pattern');
42+
throw new RuntimeException('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package: composer require php-llm/fabric-pattern');
4243
}
4344

4445
$content = (new Pattern())->load($pattern);

tests/Platform/Fabric/FabricInputProcessorTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace PhpLlm\LlmChain\Tests\Platform\Fabric;
66

77
use PhpLlm\LlmChain\Chain\Input;
8+
use PhpLlm\LlmChain\Platform\Exception\InvalidArgumentException;
9+
use PhpLlm\LlmChain\Platform\Exception\LogicException;
10+
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
811
use PhpLlm\LlmChain\Platform\Fabric\FabricInputProcessor;
912
use PhpLlm\LlmChain\Platform\Message\MessageBag;
1013
use PhpLlm\LlmChain\Platform\Message\SystemMessage;
@@ -42,7 +45,7 @@ public function processInputWithInvalidPatternType(): void
4245
$model = new Model('test-model', []);
4346
$input = new Input($model, $messages, ['fabric_pattern' => 123]);
4447

45-
$this->expectException(\InvalidArgumentException::class);
48+
$this->expectException(InvalidArgumentException::class);
4649
$this->expectExceptionMessage('The "fabric_pattern" option must be a string');
4750

4851
$processor->processInput($input);
@@ -57,7 +60,7 @@ public function processInputThrowsExceptionWhenSystemMessageExists(): void
5760
$model = new Model('test-model', []);
5861
$input = new Input($model, $messages, ['fabric_pattern' => 'test_pattern']);
5962

60-
$this->expectException(\LogicException::class);
63+
$this->expectException(LogicException::class);
6164
$this->expectExceptionMessage('Cannot add Fabric pattern: MessageBag already contains a system message');
6265

6366
$processor->processInput($input);
@@ -72,7 +75,7 @@ public function processInputThrowsExceptionWhenPackageNotInstalled(): void
7275
$model = new Model('test-model', []);
7376
$input = new Input($model, $messages, ['fabric_pattern' => 'test_pattern']);
7477

75-
$this->expectException(\RuntimeException::class);
78+
$this->expectException(RuntimeException::class);
7679
$this->expectExceptionMessage('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package');
7780

7881
$processor->processInput($input);

tests/Platform/Message/MessageFabricTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Tests\Platform\Message;
66

7+
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
78
use PhpLlm\LlmChain\Platform\Message\Message;
89
use PHPUnit\Framework\Attributes\CoversClass;
910
use PHPUnit\Framework\Attributes\Small;
@@ -17,7 +18,7 @@ final class MessageFabricTest extends TestCase
1718
#[Test]
1819
public function fabricMethodThrowsExceptionWhenPackageNotInstalled(): void
1920
{
20-
$this->expectException(\RuntimeException::class);
21+
$this->expectException(RuntimeException::class);
2122
$this->expectExceptionMessage('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package');
2223

2324
Message::fabric('test_pattern');

0 commit comments

Comments
 (0)