Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #106 from foaly-nr1/exception-listener
Browse files Browse the repository at this point in the history
Exception listener
  • Loading branch information
kleijnweb committed Nov 14, 2017
2 parents e9ab7ed + 87f83c8 commit fd313c1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use KleijnWeb\SwaggerBundle\EventListener\Response\ErrorResponseFactoryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* @author John Kleijn <john@kleijnweb.nl>
Expand Down Expand Up @@ -55,11 +56,20 @@ public function __construct(
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$error = new HttpError($event->getRequest(), $event->getException(), $this->logRefBuilder);
$request = $event->getRequest();
$exception = $event->getException();
if (!(
$request->attributes->has(RequestMeta::ATTRIBUTE_URI)
|| $exception instanceof HttpException
)) {
return;
}

$error = new HttpError($request, $exception, $this->logRefBuilder);

$this->logger->log(
$error->getSeverity(),
"{$error->getMessage()} [logref {$error->getLogRef()}]: {$event->getException()}"
"{$error->getMessage()} [logref {$error->getLogRef()}]: {$exception}"
);

$event->setResponse($this->errorResponseFactory->create($error));
Expand Down
74 changes: 62 additions & 12 deletions tests/unit/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Log\LogLevel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @author John Kleijn <john@kleijnweb.nl>
Expand Down Expand Up @@ -95,10 +96,65 @@ protected function setUp()
$this->exceptionListener = new ExceptionListener($errorResponseFactory, $logRefBuilder, $this->logger);
}

/**
* @test
*/
public function willLogExceptionsWith4xxCodesAsBadRequestNotices()
public function testWillNotHandleIfNoDocumentUriInAttributesAndNotHttpException()
{
$event = $this
->getMockBuilder(GetResponseForExceptionEvent::class)
->disableOriginalConstructor()
->setMethods(['getException', 'getRequest', 'setResponse'])
->getMock();

$event
->expects($this->any())
->method('getException')
->willReturn(new \Exception("Mary had a little lamb"))
;

$event
->expects($this->any())
->method('getRequest')
->willReturn(new Request())
;

$event
->expects($this->never())
->method('setResponse')
;

/** @var GetResponseForExceptionEvent $event */
$this->exceptionListener->onKernelException($event);
}

public function testWillHandleIfNoDocumentUriInAttributesButHttpException()
{
$event = $this
->getMockBuilder(GetResponseForExceptionEvent::class)
->disableOriginalConstructor()
->setMethods(['getException', 'getRequest', 'setResponse'])
->getMock();

$event
->expects($this->any())
->method('getException')
->willReturn(new NotFoundHttpException())
;

$event
->expects($this->any())
->method('getRequest')
->willReturn(new Request())
;

$event
->expects($this->once())
->method('setResponse')
;

/** @var GetResponseForExceptionEvent $event */
$this->exceptionListener->onKernelException($event);
}

public function testWillLogExceptionsWith4xxCodesAsBadRequestNotices()
{
for ($i = 0; $i < 99; $i++) {
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
Expand All @@ -114,10 +170,7 @@ public function willLogExceptionsWith4xxCodesAsBadRequestNotices()
}
}

/**
* @test
*/
public function willLogExceptionsWith5xxCodesAsRuntimeErrors()
public function testWillLogExceptionsWith5xxCodesAsRuntimeErrors()
{
for ($i = 0; $i < 99; $i++) {
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
Expand All @@ -133,10 +186,7 @@ public function willLogExceptionsWith5xxCodesAsRuntimeErrors()
}
}

/**
* @test
*/
public function willLogExceptionsWithUnexpectedCodesAsCriticalErrors()
public function testWillLogExceptionsWithUnexpectedCodesAsCriticalErrors()
{
$sample = [4096, 777, 22, 5, 0];
foreach ($sample as $code) {
Expand Down

0 comments on commit fd313c1

Please sign in to comment.