Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
}

if ($this->debug) {
$this->logger?->error('An exception occured, transforming to an Error resource.', ['exception' => $exception, 'operation' => $apiOperation]);
$this->logger?->debug('An exception occured, transforming to an Error resource.', ['exception' => $exception, 'operation' => $apiOperation]);
}

$dup = parent::duplicateRequest($exception, $request);
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Tests/EventListener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\State\ApiResource\Error;
use ApiPlatform\Symfony\EventListener\ErrorListener;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
Expand Down Expand Up @@ -159,4 +160,42 @@ public function testStateIsNeverModified(): void

$this->assertEquals($initialController, $controllerProp->getValue($errorListener), 'The controller property must never be modified.');
}

/**
* The listener announces that it is converting an exception into an Error
* resource. That is normal operation, and the call is already gated behind
* $debug, so it must not be reported at "error" level: with Symfony's
* fallback stderr logger (no Monolog) the phpunit recipe's
* SHELL_VERBOSITY=-1 puts the threshold at exactly "error", which makes the
* line surface in otherwise green test suites as though something failed.
*/
public function testDiagnosticIsLoggedAtDebugLevel(): void
{
$exception = new \Exception();
$operation = new Get(name: '_api_errors_problem', priority: 0, status: 400);
$resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataCollectionFactory->method('create')
->with(Error::class)
->willReturn(
new ResourceMetadataCollection(Error::class, [new ApiResource(operations: [$operation])])
);

$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
$resourceClassResolver->method('isResourceClass')->with($exception::class)->willReturn(false);

$kernel = $this->createStub(KernelInterface::class);
$kernel->method('handle')->willReturn(new Response());

$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->never())->method('error');
$logger->expects($this->once())
->method('debug')
->with('An exception occured, transforming to an Error resource.', $this->anything());

$request = Request::create('/');
$request->attributes->set('_api_operation', new Get());
$exceptionEvent = new ExceptionEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST, $exception);
$errorListener = new ErrorListener('action', $logger, true, [], $resourceMetadataCollectionFactory, ['jsonproblem' => ['application/problem+json']], [], null, $resourceClassResolver);
$errorListener->onKernelException($exceptionEvent);
}
}
Loading