Add PSR-3 compliant exception context when logging exceptions (#300)

* Add PSR-3 exception context to caught error logs

* Apply review feedback
This commit is contained in:
Sven Münnich 2026-05-12 23:20:42 +02:00 • committed by GitHub
commit 73414d65f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,6 +27,7 @@ use Mcp\Server\Handler\Request\ReadResourceHandler;
use Mcp\Server\Session\SessionInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class ReadResourceHandlerTest extends TestCase
{
@ -34,14 +35,16 @@ class ReadResourceHandlerTest extends TestCase
private RegistryInterface&MockObject $registry;
private ReferenceHandlerInterface&MockObject $referenceHandler;
private SessionInterface&MockObject $session;
private LoggerInterface&MockObject $logger;
protected function setUp(): void
{
$this->registry = $this->createMock(RegistryInterface::class);
$this->referenceHandler = $this->createMock(ReferenceHandlerInterface::class);
$this->session = $this->createMock(SessionInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->handler = new ReadResourceHandler($this->registry, $this->referenceHandler);
$this->handler = new ReadResourceHandler($this->registry, $this->referenceHandler, $this->logger);
}
public function testSupportsReadResourceRequest(): void
@ -186,6 +189,11 @@ class ReadResourceHandlerTest extends TestCase
->with($uri)
->willThrowException($exception);
$this->logger
->expects($this->once())
->method('error')
->with('Resource not found', ['uri' => $uri, 'exception' => $exception]);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Error::class, $response);
@ -206,6 +214,11 @@ class ReadResourceHandlerTest extends TestCase
->with($uri)
->willThrowException($exception);
$this->logger
->expects($this->once())
->method('error')
->with('Error while reading resource "file://corrupted/file.txt": "Failed to read resource: corrupted data".', ['exception' => $exception]);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Error::class, $response);
@ -226,6 +239,11 @@ class ReadResourceHandlerTest extends TestCase
->with($uri)
->willThrowException($exception);
$this->logger
->expects($this->once())
->method('error')
->with('Unexpected error while reading resource "file://problematic/file.txt": "Internal database connection failed".', ['exception' => $exception]);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Error::class, $response);