Skip to content

Commit ab3d9ae

Browse files
committed
refactor: use package specific exception classes
1 parent 9a73f80 commit ab3d9ae

File tree

4 files changed

+43
-58
lines changed

4 files changed

+43
-58
lines changed

src/Capability/Prompt/PromptGetter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Mcp\Capability\Registry\ReferenceHandlerInterface;
1515
use Mcp\Capability\Registry\ReferenceProviderInterface;
16+
use Mcp\Exception\PromptGetException;
17+
use Mcp\Exception\PromptNotFoundException;
1618
use Mcp\Exception\RegistryException;
1719
use Mcp\Schema\Request\GetPromptRequest;
1820
use Mcp\Schema\Result\GetPromptResult;
@@ -25,8 +27,7 @@ final class PromptGetter implements PromptGetterInterface
2527
public function __construct(
2628
private readonly ReferenceProviderInterface $referenceProvider,
2729
private readonly ReferenceHandlerInterface $referenceHandler,
28-
) {
29-
}
30+
) {}
3031

3132
/**
3233
* @throws RegistryException
@@ -37,13 +38,17 @@ public function get(GetPromptRequest $request): GetPromptResult
3738
$reference = $this->referenceProvider->getPrompt($request->name);
3839

3940
if (null === $reference) {
40-
throw new \InvalidArgumentException(\sprintf('Prompt "%s" is not registered.', $request->name));
41+
throw new PromptNotFoundException($request);
4142
}
4243

43-
return new GetPromptResult(
44-
$reference->formatResult(
45-
$this->referenceHandler->handle($reference, $request->arguments ?? []),
46-
),
47-
);
44+
try {
45+
return new GetPromptResult(
46+
$reference->formatResult(
47+
$this->referenceHandler->handle($reference, $request->arguments ?? []),
48+
),
49+
);
50+
} catch (\Throwable $e) {
51+
throw new PromptGetException($request, $e);
52+
}
4853
}
4954
}

src/Capability/Resource/ResourceReader.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
use Mcp\Capability\Registry\ReferenceHandlerInterface;
1515
use Mcp\Capability\Registry\ReferenceProviderInterface;
16-
use Mcp\Exception\RegistryException;
16+
use Mcp\Exception\ResourceNotFoundException;
17+
use Mcp\Exception\ResourceReadException;
1718
use Mcp\Schema\Request\ReadResourceRequest;
1819
use Mcp\Schema\Result\ReadResourceResult;
1920

@@ -25,25 +26,25 @@ final class ResourceReader implements ResourceReaderInterface
2526
public function __construct(
2627
private readonly ReferenceProviderInterface $referenceProvider,
2728
private readonly ReferenceHandlerInterface $referenceHandler,
28-
) {
29-
}
29+
) {}
3030

31-
/**
32-
* @throws RegistryException
33-
*/
3431
public function read(ReadResourceRequest $request): ReadResourceResult
3532
{
3633
$reference = $this->referenceProvider->getResource($request->uri);
3734

3835
if (null === $reference) {
39-
throw new \InvalidArgumentException(\sprintf('Resource "%s" is not registered.', $request->uri));
36+
throw new ResourceNotFoundException($request);
4037
}
4138

42-
return new ReadResourceResult(
43-
$reference->formatResult(
44-
$this->referenceHandler->handle($reference, ['uri' => $request->uri]),
45-
$request->uri,
46-
),
47-
);
39+
try {
40+
return new ReadResourceResult(
41+
$reference->formatResult(
42+
$this->referenceHandler->handle($reference, ['uri' => $request->uri]),
43+
$request->uri,
44+
),
45+
);
46+
} catch (\Throwable $e) {
47+
throw new ResourceReadException($request, $e);
48+
}
4849
}
4950
}

tests/Capability/Prompt/PromptGetterTest.php

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Mcp\Capability\Registry\PromptReference;
1717
use Mcp\Capability\Registry\ReferenceHandlerInterface;
1818
use Mcp\Capability\Registry\ReferenceProviderInterface;
19+
use Mcp\Exception\PromptGetException;
20+
use Mcp\Exception\PromptNotFoundException;
1921
use Mcp\Exception\RegistryException;
2022
use Mcp\Exception\RuntimeException;
2123
use Mcp\Schema\Content\PromptMessage;
@@ -151,8 +153,8 @@ public function testGetThrowsInvalidArgumentExceptionWhenPromptNotFound(): void
151153
->expects($this->never())
152154
->method('handle');
153155

154-
$this->expectException(\InvalidArgumentException::class);
155-
$this->expectExceptionMessage('Prompt "nonexistent_prompt" is not registered.');
156+
$this->expectException(PromptNotFoundException::class);
157+
$this->expectExceptionMessage('Prompt not found for name: "nonexistent_prompt".');
156158

157159
$this->promptGetter->get($request);
158160
}
@@ -176,7 +178,7 @@ public function testGetThrowsRegistryExceptionWhenHandlerFails(): void
176178
->with($promptReference, ['param' => 'value'])
177179
->willThrowException($handlerException);
178180

179-
$this->expectException(RegistryException::class);
181+
$this->expectException(PromptGetException::class);
180182

181183
$this->promptGetter->get($request);
182184
}
@@ -204,7 +206,7 @@ public function testGetHandlesJsonExceptionDuringFormatting(): void
204206
->with($promptReference, [])
205207
->willReturn('some result');
206208

207-
$this->expectException(\JsonException::class);
209+
$this->expectException(PromptGetException::class);
208210
$this->expectExceptionMessage('JSON encoding failed');
209211

210212
$this->promptGetter->get($request);
@@ -333,31 +335,6 @@ public function testGetHandlesEmptyArrayResult(): void
333335
$this->assertCount(0, $result->messages);
334336
}
335337

336-
public function testGetHandlesDifferentExceptionTypes(): void
337-
{
338-
$request = new GetPromptRequest('error_prompt', []);
339-
$prompt = $this->createValidPrompt('error_prompt');
340-
$promptReference = new PromptReference($prompt, fn () => throw new \InvalidArgumentException('Invalid input'));
341-
$handlerException = new \InvalidArgumentException('Invalid input');
342-
343-
$this->referenceProvider
344-
->expects($this->once())
345-
->method('getPrompt')
346-
->with('error_prompt')
347-
->willReturn($promptReference);
348-
349-
$this->referenceHandler
350-
->expects($this->once())
351-
->method('handle')
352-
->with($promptReference, [])
353-
->willThrowException($handlerException);
354-
355-
$this->expectException(\InvalidArgumentException::class);
356-
$this->expectExceptionMessage('Invalid input');
357-
358-
$this->promptGetter->get($request);
359-
}
360-
361338
public function testGetWithTypedContentStructure(): void
362339
{
363340
$request = new GetPromptRequest('typed_content_prompt', []);
@@ -512,7 +489,7 @@ public function testGetThrowsRuntimeExceptionForInvalidHandlerResult(): void
512489
->with($promptReference, [])
513490
->willReturn('This is not a valid prompt format');
514491

515-
$this->expectException(RuntimeException::class);
492+
$this->expectException(PromptGetException::class);
516493
$this->expectExceptionMessage('Prompt generator method must return an array of messages.');
517494

518495
$this->promptGetter->get($request);
@@ -539,7 +516,7 @@ public function testGetThrowsRuntimeExceptionForNullHandlerResult(): void
539516
->with($promptReference, [])
540517
->willReturn(null);
541518

542-
$this->expectException(RuntimeException::class);
519+
$this->expectException(PromptGetException::class);
543520
$this->expectExceptionMessage('Prompt generator method must return an array of messages.');
544521

545522
$this->promptGetter->get($request);
@@ -566,7 +543,7 @@ public function testGetThrowsRuntimeExceptionForScalarHandlerResult(): void
566543
->with($promptReference, [])
567544
->willReturn(42);
568545

569-
$this->expectException(RuntimeException::class);
546+
$this->expectException(PromptGetException::class);
570547
$this->expectExceptionMessage('Prompt generator method must return an array of messages.');
571548

572549
$this->promptGetter->get($request);
@@ -593,7 +570,7 @@ public function testGetThrowsRuntimeExceptionForBooleanHandlerResult(): void
593570
->with($promptReference, [])
594571
->willReturn(true);
595572

596-
$this->expectException(RuntimeException::class);
573+
$this->expectException(PromptGetException::class);
597574
$this->expectExceptionMessage('Prompt generator method must return an array of messages.');
598575

599576
$this->promptGetter->get($request);
@@ -622,7 +599,7 @@ public function testGetThrowsRuntimeExceptionForObjectHandlerResult(): void
622599
->with($promptReference, [])
623600
->willReturn($objectResult);
624601

625-
$this->expectException(RuntimeException::class);
602+
$this->expectException(PromptGetException::class);
626603
$this->expectExceptionMessage('Prompt generator method must return an array of messages.');
627604

628605
$this->promptGetter->get($request);

tests/Capability/Resource/ResourceReaderTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Mcp\Capability\Registry\ResourceTemplateReference;
1818
use Mcp\Capability\Resource\ResourceReader;
1919
use Mcp\Exception\RegistryException;
20+
use Mcp\Exception\ResourceNotFoundException;
21+
use Mcp\Exception\ResourceReadException;
2022
use Mcp\Schema\Content\BlobResourceContents;
2123
use Mcp\Schema\Content\TextResourceContents;
2224
use Mcp\Schema\Request\ReadResourceRequest;
@@ -239,8 +241,8 @@ public function testReadResourceThrowsExceptionWhenResourceNotFound(): void
239241
->expects($this->never())
240242
->method('handle');
241243

242-
$this->expectException(\InvalidArgumentException::class);
243-
$this->expectExceptionMessage('Resource "nonexistent://resource" is not registered.');
244+
$this->expectException(ResourceNotFoundException::class);
245+
$this->expectExceptionMessage('Resource not found for uri: "nonexistent://resource".');
244246

245247
$this->resourceReader->read($request);
246248
}
@@ -264,7 +266,7 @@ public function testReadResourceThrowsRegistryExceptionWhenHandlerFails(): void
264266
->with($resourceReference, ['uri' => 'failing://resource'])
265267
->willThrowException($handlerException);
266268

267-
$this->expectException(RegistryException::class);
269+
$this->expectException(ResourceReadException::class);
268270
$this->expectExceptionMessage('Handler execution failed');
269271

270272
$this->resourceReader->read($request);

0 commit comments

Comments
 (0)